From c758d63138e20d8bcc9f55e36dac1f2757e6eedb Mon Sep 17 00:00:00 2001 From: nagachika Date: Sat, 23 May 2015 17:01:17 +0000 Subject: [PATCH] merge revision(s) 49999,50000: [Backport #10979] * hash.c (rb_any_hash): use same hash values with Float#hash so that -0.0 and +0.0 will be identical. [ruby-core:68541] [Bug #10979] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_2@50619 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 6 ++++++ hash.c | 5 ++++- internal.h | 1 + numeric.c | 8 ++++++-- test/ruby/test_float.rb | 8 ++++++++ version.h | 6 +++--- 6 files changed, 28 insertions(+), 6 deletions(-) diff --git a/ChangeLog b/ChangeLog index 23c444d8b3..7b11b00db9 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +Sun May 24 02:01:07 2015 Nobuyoshi Nakada + + * hash.c (rb_any_hash): use same hash values with Float#hash so + that -0.0 and +0.0 will be identical. + [ruby-core:68541] [Bug #10979] + Thu May 21 01:34:48 2015 Nobuyoshi Nakada * gc.c (id2ref): prohibit from accessing internal objects. diff --git a/hash.c b/hash.c index 8d37dc6368..3ef0ed458f 100644 --- a/hash.c +++ b/hash.c @@ -142,13 +142,16 @@ rb_any_hash(VALUE a) } else if (FLONUM_P(a)) { /* prevent pathological behavior: [Bug #10761] */ - a = (st_index_t)rb_float_value(a); + return rb_dbl_hash(rb_float_value(a)); } hnum = rb_objid_hash((st_index_t)a); } else if (BUILTIN_TYPE(a) == T_STRING) { hnum = rb_str_hash(a); } + else if (BUILTIN_TYPE(a) == T_FLOAT) { + return rb_dbl_hash(rb_float_value(a)); + } else { hval = rb_hash(a); hnum = FIX2LONG(hval); diff --git a/internal.h b/internal.h index 4da45a4712..1833dca67b 100644 --- a/internal.h +++ b/internal.h @@ -773,6 +773,7 @@ double ruby_float_mod(double x, double y); int rb_num_negative_p(VALUE); VALUE rb_int_succ(VALUE num); VALUE rb_int_pred(VALUE num); +VALUE rb_dbl_hash(double d); #if USE_FLONUM #define RUBY_BIT_ROTL(v, n) (((v) << (n)) | ((v) >> ((sizeof(v) * 8) - n))) diff --git a/numeric.c b/numeric.c index 0222c76e29..e7435bbbc8 100644 --- a/numeric.c +++ b/numeric.c @@ -1137,10 +1137,14 @@ flo_eq(VALUE x, VALUE y) static VALUE flo_hash(VALUE num) { - double d; + return rb_dbl_hash(RFLOAT_VALUE(num)); +} + +VALUE +rb_dbl_hash(double d) +{ st_index_t hash; - d = RFLOAT_VALUE(num); /* normalize -0.0 to 0.0 */ if (d == 0.0) d = 0.0; hash = rb_memhash(&d, sizeof(d)); diff --git a/test/ruby/test_float.rb b/test/ruby/test_float.rb index ac2d84352b..eab4d12a29 100644 --- a/test/ruby/test_float.rb +++ b/test/ruby/test_float.rb @@ -672,4 +672,12 @@ class TestFloat < Test::Unit::TestCase assert_equal(0.0, z) assert_equal(-Float::INFINITY, 1.0/z) end + + def test_hash_0 + bug10979 = '[ruby-core:68541] [Bug #10979]' + assert_equal(+0.0.hash, -0.0.hash) + assert_operator(+0.0, :eql?, -0.0) + h = {0.0 => bug10979} + assert_equal(bug10979, h[-0.0]) + end end diff --git a/version.h b/version.h index 1db1d8a105..832802733a 100644 --- a/version.h +++ b/version.h @@ -1,10 +1,10 @@ #define RUBY_VERSION "2.2.3" -#define RUBY_RELEASE_DATE "2015-05-21" -#define RUBY_PATCHLEVEL 115 +#define RUBY_RELEASE_DATE "2015-05-24" +#define RUBY_PATCHLEVEL 116 #define RUBY_RELEASE_YEAR 2015 #define RUBY_RELEASE_MONTH 5 -#define RUBY_RELEASE_DAY 21 +#define RUBY_RELEASE_DAY 24 #include "ruby/version.h"