<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -226,7 +226,10 @@ int dictAdd(dict *ht, void *key, void *val)
     return DICT_OK;
 }
 
-/* Add an element, discarding the old if the key already exists */
+/* Add an element, discarding the old if the key already exists.
+ * Return 1 if the key was added from scratch, 0 if there was already an
+ * element with such key and dictReplace() just performed a value update
+ * operation. */
 int dictReplace(dict *ht, void *key, void *val)
 {
     dictEntry *entry;
@@ -234,13 +237,13 @@ int dictReplace(dict *ht, void *key, void *val)
     /* Try to add the element. If the key
      * does not exists dictAdd will suceed. */
     if (dictAdd(ht, key, val) == DICT_OK)
-        return DICT_OK;
+        return 1;
     /* It already exists, get the entry */
     entry = dictFind(ht, key);
     /* Free the old value and set the new one */
     dictFreeEntryVal(ht, entry);
     dictSetHashVal(ht, entry, val);
-    return DICT_OK;
+    return 0;
 }
 
 /* Search and remove an element */</diff>
      <filename>dict.c</filename>
    </modified>
    <modified>
      <diff>@@ -4664,7 +4664,9 @@ static void sortCommand(redisClient *c) {
                 }
             }
         }
-        dictReplace(c-&gt;db-&gt;dict,storekey,listObject);
+        if (dictReplace(c-&gt;db-&gt;dict,storekey,listObject)) {
+            incrRefCount(storekey);
+        }
         /* Note: we add 1 because the DB is dirty anyway since even if the
          * SORT result is empty a new key is set and maybe the old content
          * replaced. */</diff>
      <filename>redis.c</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>d0ccebcf46a2f726117b05f2a317b0a5e7113cae</id>
    </parent>
  </parents>
  <author>
    <name>root</name>
    <email>root@yserv.com</email>
  </author>
  <url>http://github.com/antirez/redis/commit/121796f792c4d8fdc06fe664d675ec16791f6818</url>
  <id>121796f792c4d8fdc06fe664d675ec16791f6818</id>
  <committed-date>2009-11-04T02:59:27-08:00</committed-date>
  <authored-date>2009-11-04T02:59:27-08:00</authored-date>
  <message>fixed a refcounting bug with SORT ... STORE leading to random crashes</message>
  <tree>f3521c83712105007a914077d7447b1563f54304</tree>
  <committer>
    <name>root</name>
    <email>root@yserv.com</email>
  </committer>
</commit>
