summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorantirez <antirez@gmail.com>2012-09-20 16:33:36 +0200
committerantirez <antirez@gmail.com>2012-09-21 11:55:32 +0200
commit578c94597fda163eb6050ce5e0f832a6587a14b9 (patch)
treed1fa2ec0a72b88979e0f0b32bd368dc1aaa87c3d
parentbe90c803e3bb041335ea0b8f40348965846f06b7 (diff)
downloadredis-578c94597fda163eb6050ce5e0f832a6587a14b9.tar.gz
SRANDMEMBER <count> leak fixed.
For "CASE 4" (see code) we need to free the element if it's already in the result dictionary and adding it failed.
-rw-r--r--src/t_set.c18
1 files changed, 10 insertions, 8 deletions
diff --git a/src/t_set.c b/src/t_set.c
index 33e7bb387..fada1095a 100644
--- a/src/t_set.c
+++ b/src/t_set.c
@@ -476,19 +476,21 @@ void srandmemberWithCountCommand(redisClient *c) {
unsigned long added = 0;
while(added < count) {
- int retval;
-
encoding = setTypeRandomElement(set,&ele,&llele);
if (encoding == REDIS_ENCODING_INTSET) {
- retval = dictAdd(d,createStringObjectFromLongLong(llele),NULL);
+ ele = createStringObjectFromLongLong(llele);
} else if (ele->encoding == REDIS_ENCODING_RAW) {
- retval = dictAdd(d,dupStringObject(ele),NULL);
+ ele = dupStringObject(ele);
} else if (ele->encoding == REDIS_ENCODING_INT) {
- retval = dictAdd(d,
- createStringObjectFromLongLong((long)ele->ptr),NULL);
+ ele = createStringObjectFromLongLong((long)ele->ptr);
}
-
- if (retval == DICT_OK) added++;
+ /* Try to add the object to the dictionary. If it already exists
+ * free it, otherwise increment the number of objects we have
+ * in the result dictionary. */
+ if (dictAdd(d,ele,NULL) == DICT_OK)
+ added++;
+ else
+ decrRefCount(ele);
}
}