diff options
author | sundb <sundbcn@gmail.com> | 2021-09-24 22:36:26 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-09-24 17:36:26 +0300 |
commit | 9967a53f4c8e9f50a4e018aee469bf8c5a4647e9 (patch) | |
tree | 6e682cfe5e1957628d515c2113856b556094f4da /src/t_zset.c | |
parent | bdecbd30df83b838bce1cb06743611abc129b208 (diff) | |
download | redis-9967a53f4c8e9f50a4e018aee469bf8c5a4647e9.tar.gz |
Use dictGetFairRandomKey() for HRANDFIELD,SRANDMEMBER,ZRANDMEMBER (#9538)
In the `HRANDFIELD`, `SRANDMEMBER` and `ZRANDMEMBER` commands,
There are some strategies that could in some rare cases return an unfair random.
these cases are where s small dict happens be be hashed unevenly.
Specifically when `count*ZRANDMEMBER_SUB_STRATEGY_MUL > size`,
using `dictGetRandomKey` to randomize from a dict will result in an unfair random result.
Diffstat (limited to 'src/t_zset.c')
-rw-r--r-- | src/t_zset.c | 2 |
1 files changed, 1 insertions, 1 deletions
diff --git a/src/t_zset.c b/src/t_zset.c index 25c48c2be..26a65baae 100644 --- a/src/t_zset.c +++ b/src/t_zset.c @@ -4159,7 +4159,7 @@ void zrandmemberWithCountCommand(client *c, long l, int withscores) { /* Remove random elements to reach the right count. */ while (size > count) { dictEntry *de; - de = dictGetRandomKey(d); + de = dictGetFairRandomKey(d); dictUnlink(d,dictGetKey(de)); sdsfree(dictGetKey(de)); dictFreeUnlinkedEntry(d,de); |