From 59222ecfbd1d46648b93897cc1efa3097f2e87e5 Mon Sep 17 00:00:00 2001 From: antirez Date: Tue, 11 Jul 2017 15:49:09 +0200 Subject: [PATCH] CLUSTER GETKEYSINSLOT: avoid overallocating. Close #3911. --- src/cluster.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/cluster.c b/src/cluster.c index 407ddee82c52e..a516e911fc944 100644 --- a/src/cluster.c +++ b/src/cluster.c @@ -4380,6 +4380,11 @@ void clusterCommand(client *c) { return; } + /* Avoid allocating more than needed in case of large COUNT argument + * and smaller actual number of keys. */ + unsigned int keys_in_slot = countKeysInSlot(slot); + if (maxkeys > keys_in_slot) maxkeys = keys_in_slot; + keys = zmalloc(sizeof(robj*)*maxkeys); numkeys = getKeysInSlot(slot, keys, maxkeys); addReplyMultiBulkLen(c,numkeys);