summaryrefslogtreecommitdiff
path: root/src/t_zset.c
diff options
context:
space:
mode:
authorsundb <sundbcn@gmail.com>2021-09-24 22:36:26 +0800
committerGitHub <noreply@github.com>2021-09-24 17:36:26 +0300
commit9967a53f4c8e9f50a4e018aee469bf8c5a4647e9 (patch)
tree6e682cfe5e1957628d515c2113856b556094f4da /src/t_zset.c
parentbdecbd30df83b838bce1cb06743611abc129b208 (diff)
downloadredis-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.c2
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);