From 2e28fb7945f8bae71bdf5844d79eb88be6cebdd7 Mon Sep 17 00:00:00 2001 From: madolson Date: Wed, 16 Sep 2026 14:41:49 +0000 Subject: [PATCH] Fix execGetKeys() dropping the first 256 keys past the static buffer execGetKeys() left result->numkeys at 0 until after its loop, but getKeysPrepareResult() copies result->numkeys entries when it grows off keysbuf, so the first MAX_KEYS_BUFFER entries were discarded and replaced with uninitialised heap. COMMAND GETKEYS EXEC with more than 256 conditions returned argv[0] for those entries, and crashed in the reply builder once the heap was no longer zeroed. Publish the count as each entry is written. Signed-off-by: madolson --- src/multi.c | 7 +++++-- tests/unit/multi.tcl | 15 +++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/multi.c b/src/multi.c index 052b39751..a12a147fd 100644 --- a/src/multi.c +++ b/src/multi.c @@ -246,9 +246,12 @@ int execGetKeys(struct serverCommand *cmd, robj **argv, int argc, getKeysResult keys = getKeysPrepareResult(result, numkeys + 1); keys[numkeys].pos = key_index; keys[numkeys].flags = CMD_KEY_RO | CMD_KEY_ACCESS; - numkeys++; + /* Publish the count as we go. getKeysPrepareResult() copies only + * result->numkeys entries when it moves off the static buffer, so + * leaving it at 0 until the end discards the first MAX_KEYS_BUFFER + * entries and hands back uninitialised ones. */ + result->numkeys = ++numkeys; } - result->numkeys = numkeys; return numkeys; } diff --git a/tests/unit/multi.tcl b/tests/unit/multi.tcl index 4048501fa..54ee12104 100644 --- a/tests/unit/multi.tcl +++ b/tests/unit/multi.tcl @@ -48,6 +48,21 @@ start_server {tags {"multi"}} { list $committed $ifeq_failed $ifne_failed $nx_failed $xx_failed [r get destination{t}] } {OK {} {} {} {} committed} + test {EXEC conditions past the static key buffer report the real keys} { + # getKeysPrepareResult() grows out of keysbuf at MAX_KEYS_BUFFER (256), + # and copies result->numkeys entries when it does. + set conds {} + for {set i 0} {$i < 300} {incr i} { + lappend conds IFEQ key:$i val:$i + } + set keys [r command getkeys EXEC {*}$conds] + assert_equal 300 [llength $keys] + assert_equal key:0 [lindex $keys 0] + assert_equal key:255 [lindex $keys 255] + assert_equal key:299 [lindex $keys 299] + r ping + } {PONG} + test {EXEC IFNE matches a missing key} { r del condition{t} destination{t} r multi