Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid computing strlen() inside loops #16584

Merged
merged 3 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions cmd/zdb/zdb.c
Original file line number Diff line number Diff line change
Expand Up @@ -8788,7 +8788,7 @@ zdb_read_block(char *thing, spa_t *spa)
void *lbuf, *buf;
char *s, *p, *dup, *flagstr, *sizes, *tmp = NULL;
const char *vdev, *errmsg = NULL;
int i, error;
int i, len, error;
boolean_t borrowed = B_FALSE, found = B_FALSE;

dup = strdup(thing);
Expand Down Expand Up @@ -8816,7 +8816,8 @@ zdb_read_block(char *thing, spa_t *spa)
for (s = strtok_r(flagstr, ":", &tmp);
s != NULL;
s = strtok_r(NULL, ":", &tmp)) {
for (i = 0; i < strlen(flagstr); i++) {
len = strlen(flagstr);
for (i = 0; i < len; i++) {
int bit = flagbits[(uchar_t)flagstr[i]];

if (bit == 0) {
Expand Down Expand Up @@ -9086,13 +9087,14 @@ zdb_embedded_block(char *thing)
static boolean_t
zdb_numeric(char *str)
{
int i = 0;
int i = 0, len;

if (strlen(str) == 0)
len = strlen(str);
if (len == 0)
return (B_FALSE);
if (strncmp(str, "0x", 2) == 0 || strncmp(str, "0X", 2) == 0)
i = 2;
for (; i < strlen(str); i++) {
for (; i < len; i++) {
if (!isxdigit(str[i]))
return (B_FALSE);
}
Expand Down
8 changes: 5 additions & 3 deletions cmd/ztest.c
Original file line number Diff line number Diff line change
Expand Up @@ -685,15 +685,17 @@ static int
str2shift(const char *buf)
{
const char *ends = "BKMGTPEZ";
int i;
int i, len;

if (buf[0] == '\0')
return (0);
for (i = 0; i < strlen(ends); i++) {

len = strlen(ends);
for (i = 0; i < len; i++) {
if (toupper(buf[0]) == ends[i])
break;
}
if (i == strlen(ends)) {
if (i == len) {
(void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n",
buf);
usage(B_FALSE);
Expand Down
8 changes: 5 additions & 3 deletions lib/libzfs/libzfs_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -1618,15 +1618,17 @@ static int
str2shift(libzfs_handle_t *hdl, const char *buf)
{
const char *ends = "BKMGTPEZ";
int i;
int i, len;

if (buf[0] == '\0')
return (0);
for (i = 0; i < strlen(ends); i++) {

len = strlen(ends);
for (i = 0; i < len; i++) {
if (toupper(buf[0]) == ends[i])
break;
}
if (i == strlen(ends)) {
if (i == len) {
if (hdl)
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
"invalid numeric suffix '%s'"), buf);
Expand Down
5 changes: 3 additions & 2 deletions module/zcommon/zfs_prop.c
Original file line number Diff line number Diff line change
Expand Up @@ -804,11 +804,12 @@ zfs_name_to_prop(const char *propname)
boolean_t
zfs_prop_user(const char *name)
{
int i;
int i, len;
char c;
boolean_t foundsep = B_FALSE;

for (i = 0; i < strlen(name); i++) {
len = strlen(name);
for (i = 0; i < len; i++) {
c = name[i];
if (!zprop_valid_char(c))
return (B_FALSE);
Expand Down
5 changes: 3 additions & 2 deletions module/zcommon/zpool_prop.c
Original file line number Diff line number Diff line change
Expand Up @@ -494,11 +494,12 @@ vdev_name_to_prop(const char *propname)
boolean_t
vdev_prop_user(const char *name)
{
int i;
int i, len;
char c;
boolean_t foundsep = B_FALSE;

for (i = 0; i < strlen(name); i++) {
len = strlen(name);
for (i = 0; i < len; i++) {
c = name[i];
if (!zprop_valid_char(c))
return (B_FALSE);
Expand Down
7 changes: 5 additions & 2 deletions udev/zvol_id.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ main(int argc, const char *const *argv)
return (1);
}
const char *dev_name = argv[1];
size_t i, len;

int fd;
struct stat sb;
Expand All @@ -73,11 +74,13 @@ main(int argc, const char *const *argv)
}

const char *dev_part = strrchr(dev_name, 'p');
len = strlen(zvol_name);
if (dev_part != NULL) {
sprintf(zvol_name + strlen(zvol_name), "-part%s", dev_part + 1);
sprintf(zvol_name + len, "-part%s", dev_part + 1);
len = strlen(zvol_name);
}

for (size_t i = 0; i < strlen(zvol_name); ++i)
for (i = 0; i < len; ++i)
if (isblank(zvol_name[i]))
zvol_name[i] = '+';

Expand Down
Loading