Skip to content

Commit 0a9286c

Browse files
committed
CLEANUP: Refactored the ntokens checking of arithmetic command.
1 parent 1bb2168 commit 0a9286c

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

memcached.c

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9084,20 +9084,27 @@ static void process_arithmetic_command(conn *c, token_t *tokens, const size_t nt
90849084
return;
90859085
}
90869086

9087+
bool bad_line = false;
90879088
bool create = false;
90889089
unsigned int flags = 0;
90899090
int64_t exptime = 0;
90909091
uint64_t init_value = 0;
90919092

90929093
if (ntokens >= 7) {
9093-
if (! (safe_strtoul(tokens[3].value, (uint32_t *)&flags)
9094-
&& safe_strtoll(tokens[4].value, &exptime)
9095-
&& safe_strtoull(tokens[5].value, &init_value))) {
9096-
print_invalid_command(c, tokens, ntokens);
9097-
out_string(c, "CLIENT_ERROR bad command line format");
9098-
return;
9094+
if (safe_strtoul(tokens[3].value, (uint32_t *)&flags)
9095+
&& safe_strtoll(tokens[4].value, &exptime)
9096+
&& safe_strtoull(tokens[5].value, &init_value)) {
9097+
create = true;
9098+
} else {
9099+
bad_line = true;
90999100
}
9100-
create = true;
9101+
} else if (ntokens == 6) {
9102+
bad_line = true;
9103+
}
9104+
if (bad_line) {
9105+
print_invalid_command(c, tokens, ntokens);
9106+
out_string(c, "CLIENT_ERROR bad command line format");
9107+
return;
91019108
}
91029109

91039110
if (settings.detail_enabled) {
@@ -13992,12 +13999,12 @@ static void process_command_ascii(conn *c, char *command, int cmdlen)
1399213999
{
1399314000
process_update_command(c, tokens, ntokens, (ENGINE_STORE_OPERATION)comm, true);
1399414001
}
13995-
else if ((ntokens == 4 || ntokens == 5 || ntokens == 7 || ntokens == 8) &&
14002+
else if ((ntokens >= 4 && ntokens <= 8) &&
1399614003
(strcmp(tokens[COMMAND_TOKEN].value, "incr") == 0))
1399714004
{
1399814005
process_arithmetic_command(c, tokens, ntokens, 1);
1399914006
}
14000-
else if ((ntokens == 4 || ntokens == 5 || ntokens == 7 || ntokens == 8) &&
14007+
else if ((ntokens >= 4 && ntokens <= 8) &&
1400114008
(strcmp(tokens[COMMAND_TOKEN].value, "decr") == 0))
1400214009
{
1400314010
process_arithmetic_command(c, tokens, ntokens, 0);

0 commit comments

Comments
 (0)