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).
This commit is contained in:
sundb 2021-02-08 18:41:16 +08:00 committed by GitHub
parent 88272cf7ac
commit 1f12be3072
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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);