]> git.saurik.com Git - redis.git/commitdiff
hincrby should report an error when called against a hash key that doesn't contain...
authorPieter Noordhuis <pcnoordhuis@gmail.com>
Thu, 6 May 2010 18:35:00 +0000 (20:35 +0200)
committerPieter Noordhuis <pcnoordhuis@gmail.com>
Thu, 6 May 2010 18:38:50 +0000 (20:38 +0200)
redis.c
test-redis.tcl

diff --git a/redis.c b/redis.c
index 05f1c64a4f2bab232315fc81ca25c5ce26c000f0..465398ada852a50c1d2fa1d3593bd634bed50439 100644 (file)
--- a/redis.c
+++ b/redis.c
@@ -3221,7 +3221,7 @@ static int getDoubleFromObject(robj *o, double *target) {
         } else if (o->encoding == REDIS_ENCODING_INT) {
             value = (long)o->ptr;
         } else {
-            redisAssert(1 != 1);
+            redisPanic("Unknown string encoding");
         }
     }
 
@@ -3258,7 +3258,7 @@ static int getLongLongFromObject(robj *o, long long *target) {
         } else if (o->encoding == REDIS_ENCODING_INT) {
             value = (long)o->ptr;
         } else {
-            redisAssert(1 != 1);
+            redisPanic("Unknown string encoding");
         }
     }
 
@@ -6462,12 +6462,11 @@ static void hincrbyCommand(redisClient *c) {
     if (getLongLongFromObjectOrReply(c,c->argv[3],&incr,NULL) != REDIS_OK) return;
     if ((o = hashLookupWriteOrCreate(c,c->argv[1])) == NULL) return;
     if ((current = hashGet(o,c->argv[2])) != NULL) {
-        if (current->encoding == REDIS_ENCODING_RAW)
-            value = strtoll(current->ptr,NULL,10);
-        else if (current->encoding == REDIS_ENCODING_INT)
-            value = (long)current->ptr;
-        else
-            redisAssert(1 != 1);
+        if (getLongLongFromObjectOrReply(c,current,&value,
+            "hash value is not an integer") != REDIS_OK) {
+            decrRefCount(current);
+            return;
+        }
         decrRefCount(current);
     } else {
         value = 0;
index 8f71c6241c68e1f1f710282253fdb44ac7d38296..d4a9ecdb017cb8cbecd45aca4e6ac35bd8a2d1b7 100644 (file)
@@ -1898,11 +1898,15 @@ proc main {} {
         list [$r hincrby smallhash tmp 17179869184] [$r hincrby bighash tmp 17179869184]
     } {34359738368 34359738368}
 
-    test {HINCRBY against key with spaces (no integer encoded)} {
-        $r hset smallhash tmp "    11    "
-        $r hset bighash tmp "    11    "
-        list [$r hincrby smallhash tmp 1] [$r hincrby bighash tmp 1]
-    } {12 12}
+    test {HINCRBY fails against hash value with spaces} {
+        $r hset smallhash str "    11    "
+        $r hset bighash str "    11    "
+        catch {$r hincrby smallhash str 1} smallerr
+        catch {$r hincrby smallhash str 1} bigerr
+        set rv {}
+        lappend rv [string match "ERR*not an integer*" $smallerr]
+        lappend rv [string match "ERR*not an integer*" $bigerr]
+    } {1 1}
 
     # TODO:
     # Randomized test, small and big