Skip to content

Commit

Permalink
fix(ruby): Fix crash when calculating Message hash values on 64-bit W…
Browse files Browse the repository at this point in the history
…indows (#8565)

* fix(ruby): Fix crash when calculating Message hash values on 64-bit Windows

* Better mapping for values outside the fixnum range

* Simpler downcasting of hash values

* Fix precedence

* Fix bundle on Ruby 2.4
  • Loading branch information
dazuma committed May 6, 2021
1 parent c8389f8 commit 7e3bbed
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
2 changes: 2 additions & 0 deletions ruby/Gemfile
@@ -1,3 +1,5 @@
source 'https://rubygems.org'

gemspec

gem "irb", "~> 1.1", "< 1.2.0" if RUBY_VERSION < "2.5"
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);
return INT2FIX(hash_value & RUBY_FIXNUM_MAX);
}

/*
Expand Down

0 comments on commit 7e3bbed

Please sign in to comment.