Skip to content

Commit

Permalink
Fix the duplicate option parsing for sorted set commands
Browse files Browse the repository at this point in the history
Signed-off-by: Shivshankar-Reddy <[email protected]>
  • Loading branch information
Shivshankar-Reddy committed Jun 5, 2024
1 parent f927565 commit a82f7bf
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/t_zset.c
Original file line number Diff line number Diff line change
Expand Up @@ -2534,6 +2534,9 @@ dictType setAccumulatorDictType = {
NULL /* allow to expand */
};

#define DUPLICATE_OPTION_WEIGHTS 1<<1
#define DUPLICATE_OPTION_AGGREGATE 1<<2

/* The zunionInterDiffGenericCommand() function is called in order to implement the
* following commands: ZUNION, ZINTER, ZDIFF, ZUNIONSTORE, ZINTERSTORE, ZDIFFSTORE,
* ZINTERCARD.
Expand All @@ -2560,6 +2563,7 @@ void zunionInterDiffGenericCommand(client *c, robj *dstkey, int numkeysIndex, in
int withscores = 0;
unsigned long cardinality = 0;
long limit = 0; /* Stop searching after reaching the limit. 0 means unlimited. */
u_int8_t dupCommandOptionFlag = 0;

/* expect setnum input keys to be given */
if ((getLongFromObjectOrReply(c, c->argv[numkeysIndex], &setnum, NULL) != C_OK)) return;
Expand Down Expand Up @@ -2610,6 +2614,12 @@ void zunionInterDiffGenericCommand(client *c, robj *dstkey, int numkeysIndex, in
while (remaining) {
if (op != SET_OP_DIFF && !cardinality_only && remaining >= (setnum + 1) &&
!strcasecmp(c->argv[j]->ptr, "weights")) {
if (dupCommandOptionFlag & DUPLICATE_OPTION_WEIGHTS) {
addReplyError(c, "syntax error, multiple weights option is not allowed");
zfree(src);
return;
}
dupCommandOptionFlag |= DUPLICATE_OPTION_WEIGHTS;
j++;
remaining--;
for (i = 0; i < setnum; i++, j++, remaining--) {
Expand All @@ -2621,6 +2631,12 @@ void zunionInterDiffGenericCommand(client *c, robj *dstkey, int numkeysIndex, in
}
} else if (op != SET_OP_DIFF && !cardinality_only && remaining >= 2 &&
!strcasecmp(c->argv[j]->ptr, "aggregate")) {
if (dupCommandOptionFlag & DUPLICATE_OPTION_AGGREGATE) {
addReplyError(c, "syntax error, multiple aggregate option is not allowed");
zfree(src);
return;
}
dupCommandOptionFlag |= DUPLICATE_OPTION_AGGREGATE;
j++;
remaining--;
if (!strcasecmp(c->argv[j]->ptr, "sum")) {
Expand All @@ -2639,6 +2655,11 @@ void zunionInterDiffGenericCommand(client *c, robj *dstkey, int numkeysIndex, in
} else if (remaining >= 1 && !dstkey && !cardinality_only && !strcasecmp(c->argv[j]->ptr, "withscores")) {
j++;
remaining--;
if (withscores) {
addReplyError(c, "syntax error, multiple withscores option is not allowed");
zfree(src);
return;
}
withscores = 1;
} else if (cardinality_only && remaining >= 2 && !strcasecmp(c->argv[j]->ptr, "limit")) {
j++;
Expand Down

0 comments on commit a82f7bf

Please sign in to comment.