valkey-io/valkey

[BUG] HGETDEL does not validate the FIELDS keyword

已關閉

#4,045 建立於 2026年6月24日

 (0 則留言) (1 個反應) (0 位負責人)C (1,138 個分叉)batch import
buggood first issue

倉庫指標

星標
 (25,779 顆星)
PR 合併指標
 (平均合併 17天 7小時) (30 天內合併 74 個 PR)

描述

Describe the bug

HGETDEL accepts any token where the literal FIELDS keyword is expected. The parser hardcodes the argument position and only validates numfields and the field count, so the syntax check for FIELDS is silently skipped. This is inconsistent with HGETEX, HSETEX, and the HEXPIRE family, which all validate the keyword via strcasecmp(..., "fields").

To reproduce

127.0.0.1:6379> HSET key field value
(integer) 1
127.0.0.1:6379> HGETDEL key persist 1 field
1) "value"
127.0.0.1:6379> HGETDEL key foobar 1 field
1) (nil)
127.0.0.1:6379> HGETDEL key "" 1 field
1) (nil)

All three HGETDEL calls succeed even though none of persist, foobar, or "" is the FIELDS keyword.

Expected behavior

A syntax error, matching the behavior of HGETEX/HSETEX/HEXPIRE when FIELDS is missing or misspelled:

(error) ERR syntax error

Additional information

Source: src/t_hash.c, hgetdelCommand (~line 1158):

void hgetdelCommand(client *c) {
    /* argv: [0]=HGETDEL, [1]=key, [2]=FIELDS, [3]=numfields, [4...]=fields */
    int fields_index = 4;
    ...
    if (getLongLongFromObjectOrReply(c, c->argv[fields_index - 1], &num_fields, NULL) != C_OK) return;

argv[2] is never inspected. Compare with hgetexCommand (~line 1644), which validates the keyword:

for (; fields_index < c->argc - 1; fields_index++) {
    if (!strcasecmp(objectGetVal(c->argv[fields_index]), "fields")) {
        ...
    }
}

The command JSON spec (src/commands/hgetdel.json) declares "token": "FIELDS" on the fields block, so the literal keyword is part of the documented syntax.

Suggested fix: add a strcasecmp(c->argv[2]->ptr, "FIELDS") check before parsing numfields, returning a syntax error otherwise. HGETDEL has no optional tokens before FIELDS, so a minimal positional check is sufficient.

Affected versions: unstable HGETDEL is since: 9.1.0 per the command JSON.

貢獻者指南