From 7e3bbeda920ccc1eb6942ec581a48ee6687a1cc7 Mon Sep 17 00:00:00 2001 From: Daniel Azuma Date: Thu, 6 May 2021 09:45:11 -0700 Subject: [PATCH] fix(ruby): Fix crash when calculating Message hash values on 64-bit Windows (#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 --- ruby/Gemfile | 2 ++ ruby/ext/google/protobuf_c/message.c | 5 ++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/ruby/Gemfile b/ruby/Gemfile index fa75df156..76b23ad91 100644 --- a/ruby/Gemfile +++ b/ruby/Gemfile @@ -1,3 +1,5 @@ source 'https://rubygems.org' gemspec + +gem "irb", "~> 1.1", "< 1.2.0" if RUBY_VERSION < "2.5" diff --git a/ruby/ext/google/protobuf_c/message.c b/ruby/ext/google/protobuf_c/message.c index 1eb0cb76d..ffdae6a40 100644 --- a/ruby/ext/google/protobuf_c/message.c +++ b/ruby/ext/google/protobuf_c/message.c @@ -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); } /*