From 1f12be30723abf6487e65557be501e54a0ed8463 Mon Sep 17 00:00:00 2001 From: sundb Date: Mon, 8 Feb 2021 18:41:16 +0800 Subject: [PATCH] Fix random probability check in ziplistRandomPairsUnique (#8467) When (remaining == (total_size - index)), element will definitely be random to. But when rand() == RAND_MAX, the element will miss, this will trigger assert in serverAssert(ziplistRandomPairsUnique(zsetobj->ptr, count, keys, vals) == count). --- src/ziplist.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ziplist.c b/src/ziplist.c index 2c0074d5d..35a0f283a 100644 --- a/src/ziplist.c +++ b/src/ziplist.c @@ -1608,7 +1608,7 @@ unsigned int ziplistRandomPairsUnique(unsigned char *zl, unsigned int count, zip while (picked < count && p) { double randomDouble = ((double)rand()) / RAND_MAX; double threshold = ((double)remaining) / (total_size - index); - if(randomDouble < threshold){ + if (randomDouble <= threshold) { assert(ziplistGet(p, &key, &klen, &klval)); ziplistSaveValue(key, klen, klval, &keys[picked]); p = ziplistNext(zl, p);