Skip to content
Browse files

Fixed ZINCR Nan bugs leading to server crash and added tests

  • Loading branch information...
1 parent 329cdba commit d1481e023a128aef6d88bdf8b43cfb604c601eb7 @antirez committed May 28, 2010
Showing with 33 additions and 0 deletions.
  1. +14 −0 redis.c
  2. +19 −0 tests/unit/type/zset.tcl
View
14 redis.c
@@ -5710,6 +5710,11 @@ static void zaddGenericCommand(redisClient *c, robj *key, robj *ele, double scor
zset *zs;
double *score;
+ if (isnan(scoreval)) {
+ addReplySds(c,sdsnew("-ERR provide score is Not A Number (nan)\r\n"));
+ return;
+ }
+
zsetobj = lookupKeyWrite(c->db,key);
if (zsetobj == NULL) {
zsetobj = createZsetObject();
@@ -5738,6 +5743,15 @@ static void zaddGenericCommand(redisClient *c, robj *key, robj *ele, double scor
} else {
*score = scoreval;
}
+ if (isnan(*score)) {
+ addReplySds(c,
+ sdsnew("-ERR resulting score is Not A Number (nan)\r\n"));
+ zfree(score);
+ /* Note that we don't need to check if the zset may be empty and
+ * should be removed here, as we can only obtain Nan as score if
+ * there was already an element in the sorted set. */
+ return;
+ }
} else {
*score = scoreval;
}
View
19 tests/unit/type/zset.tcl
@@ -381,4 +381,23 @@ start_server default.conf {} {
}
set _ $err
} {}
+
+ test {ZSET element can't be set to nan with ZADD} {
+ set e {}
+ catch {r zadd myzset nan abc} e
+ set _ $e
+ } {*Not A Number*}
+
+ test {ZSET element can't be set to nan with ZINCRBY} {
+ set e {}
+ catch {r zincrby myzset nan abc} e
+ set _ $e
+ } {*Not A Number*}
+
+ test {ZINCRBY calls leading to Nan are refused} {
+ set e {}
+ r zincrby myzset +inf abc
+ catch {r zincrby myzset -inf abc} e
+ set _ $e
+ } {*Not A Number*}
}

0 comments on commit d1481e0

Please sign in to comment.
Something went wrong with that request. Please try again.