Repository: redis/redis (2025-08-01T03:09:13Z)

Issue:
[BUG] module.c RM_GetCommandKeysWithFlags should free keysResult
**Describe the bug**

redis module RM_GetCommandKeysWithFlags should always free keys result




Discussion:
do you mean there is a memory leak?
yes, When the keys returned by cmd exceed the MAX_KEYS_BUFFER,  getKeysResult.keys will be  call zmalloc, but there will be not call free 
@brevin-yoke thx, feel free to make a PR to fix it and also create a test for it.
@brevin-yoke It's been two weeks since this issue was created. If you haven't started, do you mind if I work on it?



Patch:
diff --git a/src/module.c b/src/module.c
index 216030809aa..e1f40555dd4 100644
--- a/src/module.c
+++ b/src/module.c
@@ -14176,6 +14176,7 @@ int *RM_GetCommandKeysWithFlags(RedisModuleCtx *ctx, RedisModuleString **argv, i
             (*out_flags)[i] = moduleConvertKeySpecsFlags(result.keys[i].flags, 0);
     }
 
+    getKeysFreeResult(&result);
     return res;
 }
 


Test patch:
diff --git a/tests/unit/moduleapi/getkeys.tcl b/tests/unit/moduleapi/getkeys.tcl
index b84bb0f739c..48f97ddf430 100644
--- a/tests/unit/moduleapi/getkeys.tcl
+++ b/tests/unit/moduleapi/getkeys.tcl
@@ -58,6 +58,15 @@ start_server {tags {"modules"}} {
         set _ $e
     } {*EINVAL*}
 
+    test "introspect with > MAX_KEYS_BUFFER keys triggers RM_GetCommandKeysWithFlags heap alloc" {
+        set args {}
+        for {set i 0} {$i < 7} {incr i} {
+            lappend args key k$i
+        }
+        set reply [r getkeys.introspect 1 getkeys.command_with_flags {*}$args]
+        assert_equal {{k0 RO} {k1 RO} {k2 RO} {k3 RO} {k4 RO} {k5 RO} {k6 RO}} $reply
+    }
+
     # user that can only read from "read" keys, write to "write" keys, and read+write to "RW" keys
     r ACL setuser testuser +@all %R~read* %W~write* %RW~rw*