Skip to content

Commit 121796f

Browse files
author
root
committed
fixed a refcounting bug with SORT ... STORE leading to random crashes
1 parent d0ccebc commit 121796f

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

dict.c

+6-3
Original file line numberDiff line numberDiff line change
@@ -226,21 +226,24 @@ int dictAdd(dict *ht, void *key, void *val)
226226
return DICT_OK;
227227
}
228228

229-
/* Add an element, discarding the old if the key already exists */
229+
/* Add an element, discarding the old if the key already exists.
230+
* Return 1 if the key was added from scratch, 0 if there was already an
231+
* element with such key and dictReplace() just performed a value update
232+
* operation. */
230233
int dictReplace(dict *ht, void *key, void *val)
231234
{
232235
dictEntry *entry;
233236

234237
/* Try to add the element. If the key
235238
* does not exists dictAdd will suceed. */
236239
if (dictAdd(ht, key, val) == DICT_OK)
237-
return DICT_OK;
240+
return 1;
238241
/* It already exists, get the entry */
239242
entry = dictFind(ht, key);
240243
/* Free the old value and set the new one */
241244
dictFreeEntryVal(ht, entry);
242245
dictSetHashVal(ht, entry, val);
243-
return DICT_OK;
246+
return 0;
244247
}
245248

246249
/* Search and remove an element */

redis.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -4664,7 +4664,9 @@ static void sortCommand(redisClient *c) {
46644664
}
46654665
}
46664666
}
4667-
dictReplace(c->db->dict,storekey,listObject);
4667+
if (dictReplace(c->db->dict,storekey,listObject)) {
4668+
incrRefCount(storekey);
4669+
}
46684670
/* Note: we add 1 because the DB is dirty anyway since even if the
46694671
* SORT result is empty a new key is set and maybe the old content
46704672
* replaced. */

0 commit comments

Comments
 (0)