[前][次][番号順一覧][スレッド一覧]

ruby-changes:49296

From: usa <ko1@a...>
Date: Fri, 22 Dec 2017 17:52:15 +0900 (JST)
Subject: [ruby-changes:49296] usa:r61413 (trunk): force hash values fixable

usa	2017-12-22 17:52:11 +0900 (Fri, 22 Dec 2017)

  New Revision: 61413

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=61413

  Log:
    force hash values fixable
    
    * include/ruby/ruby.h (RB_ST2FIX): force fixable on LLP64 environment.
    
    * hash.c (any_hash): ditto.
      [ruby-core:84395] [Bug #14218]

  Modified files:
    trunk/hash.c
    trunk/include/ruby/ruby.h
    trunk/test/ruby/test_hash.rb
Index: include/ruby/ruby.h
===================================================================
--- include/ruby/ruby.h	(revision 61412)
+++ include/ruby/ruby.h	(revision 61413)
@@ -1575,7 +1575,11 @@ rb_num2char_inline(VALUE x) https://github.com/ruby/ruby/blob/trunk/include/ruby/ruby.h#L1575
 #define NUM2CHR(x) RB_NUM2CHR(x)
 #define CHR2FIX(x) RB_CHR2FIX(x)
 
+#if SIZEOF_LONG < SIZEOF_VALUE
+#define RB_ST2FIX(h) RB_LONG2FIX((long)((h) > 0 ? (h) & (unsigned long)-1 >> 2 : (h) | ~((unsigned long)-1 >> 2)))
+#else
 #define RB_ST2FIX(h) RB_LONG2FIX((long)(h))
+#endif
 #define ST2FIX(h) RB_ST2FIX(h)
 
 #define RB_ALLOC_N(type,n) ((type*)ruby_xmalloc2((size_t)(n),sizeof(type)))
Index: hash.c
===================================================================
--- hash.c	(revision 61412)
+++ hash.c	(revision 61413)
@@ -193,8 +193,16 @@ any_hash(VALUE a, st_index_t (*other_fun https://github.com/ruby/ruby/blob/trunk/hash.c#L193
 	hnum = other_func(a);
     }
   out:
+#if SIZEOF_LONG < SIZEOF_ST_INDEX_T
+    if (hnum > 0)
+	hnum &= (unsigned long)-1 >> 2;
+    else
+	hnum |= ~((unsigned long)-1 >> 2);
+#else
     hnum <<= 1;
-    return (long)RSHIFT(hnum, 1);
+    hnum = RSHIFT(hnum, 1);
+#endif
+    return (long)hnum;
 }
 
 static st_index_t
Index: test/ruby/test_hash.rb
===================================================================
--- test/ruby/test_hash.rb	(revision 61412)
+++ test/ruby/test_hash.rb	(revision 61413)
@@ -1596,6 +1596,13 @@ class TestHash < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_hash.rb#L1596
     assert_equal(%w(1.0  2.1  3.2), y.values_at(:a, :b, :c))
   end
 
+  def test_broken_hash_value
+    bug14218 = '[ruby-core:84395] [Bug #14218]'
+
+    assert_equal(0, 1_000_000.times.count{a=Object.new.hash; b=Object.new.hash; a < 0 && b < 0 && a + b > 0}, bug14218)
+    assert_equal(0, 1_000_000.times.count{a=Object.new.hash; b=Object.new.hash; 0 + a + b != 0 + b + a}, bug14218)
+  end
+
   class TestSubHash < TestHash
     class SubHash < Hash
       def reject(*)

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]