Skip to content

Commit d3b6559

Browse files
authored
Clean up unnecessary use of strlen (#1976)
Clean up unnecessary use of strlen, and reduce string2ull useless length calculations. --------- Signed-off-by: artikell <739609084@qq.com>
1 parent 5df95b3 commit d3b6559

File tree

13 files changed

+33
-32
lines changed

13 files changed

+33
-32
lines changed

src/acl.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1499,7 +1499,7 @@ unsigned long ACLGetCommandID(sds cmdname) {
14991499
sdsfree(lowername);
15001500
return (unsigned long)id;
15011501
}
1502-
raxInsert(commandId, (unsigned char *)lowername, strlen(lowername), (void *)nextid, NULL);
1502+
raxInsert(commandId, (unsigned char *)lowername, sdslen(lowername), (void *)nextid, NULL);
15031503
sdsfree(lowername);
15041504
unsigned long thisid = nextid;
15051505
nextid++;
@@ -2243,7 +2243,7 @@ static sds ACLLoadFromFile(const char *filename) {
22432243
/* Split the file into lines and attempt to load each line. */
22442244
int totlines;
22452245
sds *lines, errors = sdsempty();
2246-
lines = sdssplitlen(acls, strlen(acls), "\n", 1, &totlines);
2246+
lines = sdssplitlen(acls, sdslen(acls), "\n", 1, &totlines);
22472247
sdsfree(acls);
22482248

22492249
/* We do all the loading in a fresh instance of the Users radix tree,

src/bitops.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,7 @@ void printBits(unsigned char *p, unsigned long count) {
522522
int getBitOffsetFromArgument(client *c, robj *o, uint64_t *offset, int hash, int bits) {
523523
long long loffset;
524524
char *err = "bit offset is not an integer or out of range";
525-
char *p = o->ptr;
525+
sds p = o->ptr;
526526
size_t plen = sdslen(p);
527527
int usehash = 0;
528528

@@ -555,7 +555,8 @@ int getBitOffsetFromArgument(client *c, robj *o, uint64_t *offset, int hash, int
555555
*
556556
* On error C_ERR is returned and an error is sent to the client. */
557557
int getBitfieldTypeFromArgument(client *c, robj *o, int *sign, int *bits) {
558-
char *p = o->ptr;
558+
sds p = o->ptr;
559+
size_t plen = sdslen(p);
559560
char *err = "Invalid bitfield type. Use something like i16 u8. Note that u64 is not supported but i64 is.";
560561
long long llbits;
561562

@@ -568,7 +569,7 @@ int getBitfieldTypeFromArgument(client *c, robj *o, int *sign, int *bits) {
568569
return C_ERR;
569570
}
570571

571-
if ((string2ll(p + 1, strlen(p + 1), &llbits)) == 0 || llbits < 1 || (*sign == 1 && llbits > 64) ||
572+
if ((string2ll(p + 1, plen - 1, &llbits)) == 0 || llbits < 1 || (*sign == 1 && llbits > 64) ||
572573
(*sign == 0 && llbits > 63)) {
573574
addReplyError(c, err);
574575
return C_ERR;

src/config.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ static int updateClientOutputBufferLimit(sds *args, int arg_len, const char **er
438438
* abnormal aggregate `save T C` functionality. Remove in the future. */
439439
static int reading_config_file;
440440

441-
void loadServerConfigFromString(char *config) {
441+
void loadServerConfigFromString(sds config) {
442442
deprecatedConfig deprecated_configs[] = {
443443
{"list-max-ziplist-entries", 2, 2},
444444
{"list-max-ziplist-value", 2, 2},
@@ -455,7 +455,7 @@ void loadServerConfigFromString(char *config) {
455455
int argc;
456456

457457
reading_config_file = 1;
458-
lines = sdssplitlen(config, strlen(config), "\n", 1, &totlines);
458+
lines = sdssplitlen(config, sdslen(config), "\n", 1, &totlines);
459459

460460
for (i = 0; i < totlines; i++) {
461461
linenum = i + 1;

src/db.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,12 +1032,12 @@ void hashtableScanCallback(void *privdata, void *entry) {
10321032
if (val) listAddNodeTail(keys, val);
10331033
}
10341034

1035-
/* Try to parse a SCAN cursor stored at object 'o':
1035+
/* Try to parse a SCAN cursor stored at buffer 'buf':
10361036
* if the cursor is valid, store it as unsigned integer into *cursor and
10371037
* returns C_OK. Otherwise return C_ERR and send an error to the
10381038
* client. */
1039-
int parseScanCursorOrReply(client *c, robj *o, unsigned long long *cursor) {
1040-
if (!string2ull(o->ptr, cursor)) {
1039+
int parseScanCursorOrReply(client *c, sds buf, unsigned long long *cursor) {
1040+
if (!string2ull(buf, sdslen(buf), cursor)) {
10411041
addReplyError(c, "invalid cursor");
10421042
return C_ERR;
10431043
}
@@ -1298,7 +1298,7 @@ void scanGenericCommand(client *c, robj *o, unsigned long long cursor) {
12981298
/* The SCAN command completely relies on scanGenericCommand. */
12991299
void scanCommand(client *c) {
13001300
unsigned long long cursor;
1301-
if (parseScanCursorOrReply(c, c->argv[1], &cursor) == C_ERR) return;
1301+
if (parseScanCursorOrReply(c, c->argv[1]->ptr, &cursor) == C_ERR) return;
13021302
scanGenericCommand(c, NULL, cursor);
13031303
}
13041304

src/module.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2982,7 +2982,7 @@ int VM_StringToLongLong(const ValkeyModuleString *str, long long *ll) {
29822982
* as a valid, strict `unsigned long long` (no spaces before/after), VALKEYMODULE_ERR
29832983
* is returned. */
29842984
int VM_StringToULongLong(const ValkeyModuleString *str, unsigned long long *ull) {
2985-
return string2ull(str->ptr, ull) ? VALKEYMODULE_OK : VALKEYMODULE_ERR;
2985+
return string2ull(str->ptr, sdslen(str->ptr), ull) ? VALKEYMODULE_OK : VALKEYMODULE_ERR;
29862986
}
29872987

29882988
/* Convert the string into a double, storing it at `*d`.
@@ -10640,7 +10640,7 @@ unsigned long long VM_ServerInfoGetFieldUnsigned(ValkeyModuleServerInfoData *dat
1064010640
return 0;
1064110641
}
1064210642
sds val = result;
10643-
if (!string2ull(val, &ll)) {
10643+
if (!string2ull(val, sdslen(val), &ll)) {
1064410644
if (out_err) *out_err = VALKEYMODULE_ERR;
1064510645
return 0;
1064610646
}

src/sentinel.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4190,52 +4190,52 @@ void sentinelSetCommand(client *c) {
41904190
changes++;
41914191
} else if (!strcasecmp(option, "notification-script") && moreargs > 0) {
41924192
/* notification-script <path> */
4193-
char *value = c->argv[++j]->ptr;
4193+
sds value = c->argv[++j]->ptr;
41944194
if (sentinel.deny_scripts_reconfig) {
41954195
addReplyError(c, "Reconfiguration of scripts path is denied for "
41964196
"security reasons. Check the deny-scripts-reconfig "
41974197
"configuration directive in your Sentinel configuration");
41984198
goto seterr;
41994199
}
42004200

4201-
if (strlen(value) && access(value, X_OK) == -1) {
4201+
if (sdslen(value) && access(value, X_OK) == -1) {
42024202
addReplyError(c, "Notification script seems non existing or non executable");
42034203
goto seterr;
42044204
}
42054205
sdsfree(ri->notification_script);
4206-
ri->notification_script = strlen(value) ? sdsnew(value) : NULL;
4206+
ri->notification_script = sdslen(value) ? sdsdup(value) : NULL;
42074207
changes++;
42084208
} else if (!strcasecmp(option, "client-reconfig-script") && moreargs > 0) {
42094209
/* client-reconfig-script <path> */
4210-
char *value = c->argv[++j]->ptr;
4210+
sds value = c->argv[++j]->ptr;
42114211
if (sentinel.deny_scripts_reconfig) {
42124212
addReplyError(c, "Reconfiguration of scripts path is denied for "
42134213
"security reasons. Check the deny-scripts-reconfig "
42144214
"configuration directive in your Sentinel configuration");
42154215
goto seterr;
42164216
}
42174217

4218-
if (strlen(value) && access(value, X_OK) == -1) {
4218+
if (sdslen(value) && access(value, X_OK) == -1) {
42194219
addReplyError(c, "Client reconfiguration script seems non existing or "
42204220
"non executable");
42214221
goto seterr;
42224222
}
42234223
sdsfree(ri->client_reconfig_script);
4224-
ri->client_reconfig_script = strlen(value) ? sdsnew(value) : NULL;
4224+
ri->client_reconfig_script = sdslen(value) ? sdsdup(value) : NULL;
42254225
changes++;
42264226
} else if (!strcasecmp(option, "auth-pass") && moreargs > 0) {
42274227
/* auth-pass <password> */
4228-
char *value = c->argv[++j]->ptr;
4228+
sds value = c->argv[++j]->ptr;
42294229
sdsfree(ri->auth_pass);
4230-
ri->auth_pass = strlen(value) ? sdsnew(value) : NULL;
4230+
ri->auth_pass = sdslen(value) ? sdsdup(value) : NULL;
42314231
dropInstanceConnections(ri);
42324232
changes++;
42334233
redacted = 1;
42344234
} else if (!strcasecmp(option, "auth-user") && moreargs > 0) {
42354235
/* auth-user <username> */
4236-
char *value = c->argv[++j]->ptr;
4236+
sds value = c->argv[++j]->ptr;
42374237
sdsfree(ri->auth_user);
4238-
ri->auth_user = strlen(value) ? sdsnew(value) : NULL;
4238+
ri->auth_user = sdslen(value) ? sdsdup(value) : NULL;
42394239
dropInstanceConnections(ri);
42404240
changes++;
42414241
} else if (!strcasecmp(option, "quorum") && moreargs > 0) {

src/server.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3457,7 +3457,7 @@ int selectDb(client *c, int id);
34573457
void signalModifiedKey(client *c, serverDb *db, robj *key);
34583458
void signalFlushedDb(int dbid, int async);
34593459
void scanGenericCommand(client *c, robj *o, unsigned long long cursor);
3460-
int parseScanCursorOrReply(client *c, robj *o, unsigned long long *cursor);
3460+
int parseScanCursorOrReply(client *c, sds buf, unsigned long long *cursor);
34613461
int dbAsyncDelete(serverDb *db, robj *key);
34623462
void emptyDbAsync(serverDb *db);
34633463
size_t lazyfreeGetPendingObjectsCount(void);

src/t_hash.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1092,7 +1092,7 @@ void hscanCommand(client *c) {
10921092
robj *o;
10931093
unsigned long long cursor;
10941094

1095-
if (parseScanCursorOrReply(c, c->argv[2], &cursor) == C_ERR) return;
1095+
if (parseScanCursorOrReply(c, c->argv[2]->ptr, &cursor) == C_ERR) return;
10961096
if ((o = lookupKeyReadOrReply(c, c->argv[1], shared.emptyscan)) == NULL || checkType(c, o, OBJ_HASH)) return;
10971097
scanGenericCommand(c, o, cursor);
10981098
}

src/t_set.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1650,7 +1650,7 @@ void sscanCommand(client *c) {
16501650
robj *set;
16511651
unsigned long long cursor;
16521652

1653-
if (parseScanCursorOrReply(c, c->argv[2], &cursor) == C_ERR) return;
1653+
if (parseScanCursorOrReply(c, c->argv[2]->ptr, &cursor) == C_ERR) return;
16541654
if ((set = lookupKeyReadOrReply(c, c->argv[1], shared.emptyscan)) == NULL || checkType(c, set, OBJ_SET)) return;
16551655
scanGenericCommand(c, set, cursor);
16561656
}

src/t_stream.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1894,14 +1894,14 @@ int streamGenericParseIDOrReply(client *c,
18941894
unsigned long long ms, seq;
18951895
char *dot = strchr(buf, '-');
18961896
if (dot) *dot = '\0';
1897-
if (string2ull(buf, &ms) == 0) goto invalid;
1897+
if (string2ull(buf, strlen(buf), &ms) == 0) goto invalid;
18981898
if (dot) {
18991899
size_t seqlen = strlen(dot + 1);
19001900
if (seq_given != NULL && seqlen == 1 && *(dot + 1) == '*') {
19011901
/* Handle the <ms>-* form. */
19021902
seq = 0;
19031903
*seq_given = 0;
1904-
} else if (string2ull(dot + 1, &seq) == 0) {
1904+
} else if (string2ull(dot + 1, seqlen, &seq) == 0) {
19051905
goto invalid;
19061906
}
19071907
} else {

0 commit comments

Comments
 (0)