-
Notifications
You must be signed in to change notification settings - Fork 873
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(server): Do not crash HRANDFIELD
if some/all elements expired
#2113
Conversation
|
I decided to, indeed, go with the slightly more complicated solution. It's not too bad, and definitely a better & safer API. |
@@ -142,6 +162,8 @@ void StringMap::RandomPairsUnique(unsigned int count, std::vector<sds>& keys, | |||
|
|||
void StringMap::RandomPairs(unsigned int count, std::vector<sds>& keys, std::vector<sds>& vals, | |||
bool with_value) { | |||
CollectExpired(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe add a comment, this is O(n)
function that may slow down RandomPairs
for large sets?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
friendly ping :) |
The issue is twofold:
Size()
will crash. However, we cannot know this in advance, asbegin()
will actually invalidate all elements, but we're already insideRandomPair()
which must return a result (it should not be called if real size is 0, after expiration)operator +=
before we know how many real elements are in the array. Today we do+= rand() / Size()
, and this in fact is problematic even ifSize()
is non-zero, but is greater than post-expiration sizeUnfortunately, this solution will iterate over all elements in map for the multi-random retrieval function, to know how many non-expired elements (if any) are in the list. I don't think it's too bad, as we iterate there anyway (although not necessarily through all)
Fixes #2108 and other bugs.