Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
ruanhaishen
redis
Commits
2783aa1d
Commit
2783aa1d
authored
Jan 26, 2023
by
Vitaly Arbuzov
Browse files
Fix randomkey NPE when DB is empty in cluster mode
parent
c45570f4
Changes
1
Show whitespace changes
Inline
Side-by-side
src/db.c
View file @
2783aa1d
...
@@ -394,7 +394,8 @@ dict *getRandomDict(redisDb *db) {
...
@@ -394,7 +394,8 @@ dict *getRandomDict(redisDb *db) {
target
|=
rand
();
target
|=
rand
();
}
}
unsigned
long
long
int
key_count
=
dbSize
(
db
);
unsigned
long
long
int
key_count
=
dbSize
(
db
);
target
=
key_count
?
target
%
key_count
:
0
;
/* Random key index in range [0..KEY_COUNT), or 0 if db is empty. */
if
(
!
key_count
)
return
db
->
dict
[
0
];
target
%=
key_count
;
/* Random key index in range [0..KEY_COUNT). */
/* In linearly enumerated key space, find a slot that contains target key. */
/* In linearly enumerated key space, find a slot that contains target key. */
dbIterator
dbit
;
dbIterator
dbit
;
dbInitIterator
(
&
dbit
,
db
);
dbInitIterator
(
&
dbit
,
db
);
...
@@ -402,11 +403,11 @@ dict *getRandomDict(redisDb *db) {
...
@@ -402,11 +403,11 @@ dict *getRandomDict(redisDb *db) {
while
((
d
=
dbNextDict
(
&
dbit
)))
{
while
((
d
=
dbNextDict
(
&
dbit
)))
{
unsigned
long
long
int
ds
=
dictSize
(
d
);
unsigned
long
long
int
ds
=
dictSize
(
d
);
if
(
target
<
ds
)
{
/* Found dict that contains target key. */
if
(
target
<
ds
)
{
/* Found dict that contains target key. */
b
re
ak
;
re
turn
d
;
}
}
target
-=
ds
;
target
-=
ds
;
}
}
return
d
;
serverPanic
(
"Bug in random dict selection, iteration completed without finding a slot for the target element."
)
;
}
}
/* Helper for sync and async delete. */
/* Helper for sync and async delete. */
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment