Skip to content
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(ruby): Fix crash when calculating Message hash values on 64-bit Windows #8565

Merged
merged 5 commits into from May 6, 2021
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion ruby/ext/google/protobuf_c/message.c
Expand Up @@ -734,7 +734,10 @@ uint64_t Message_Hash(const upb_msg* msg, const upb_msgdef* m, uint64_t seed) {
*/
static VALUE Message_hash(VALUE _self) {
Message* self = ruby_to_Message(_self);
return INT2FIX(Message_Hash(self->msg, self->msgdef, 0));
uint64_t hash_value = Message_Hash(self->msg, self->msgdef, 0);
// RUBY_FIXNUM_MAX should be one less than a power of 2.
assert(RUBY_FIXNUM_MAX & (RUBY_FIXNUM_MAX + 1) == 0);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, this is my bad, I always forget that the precedence of & is low. It should be:

assert((RUBY_FIXNUM_MAX & (RUBY_FIXNUM_MAX + 1)) == 0);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoops, I forgot that too. Fixed.

return INT2FIX(hash_value & RUBY_FIXNUM_MAX);
}

/*
Expand Down