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

ruby-changes:33561

From: tarui <ko1@a...>
Date: Sun, 20 Apr 2014 12:58:27 +0900 (JST)
Subject: [ruby-changes:33561] tarui:r45642 (trunk): * st.c (st_foreach_check): chnage start point of search at check

tarui	2014-04-20 12:58:22 +0900 (Sun, 20 Apr 2014)

  New Revision: 45642

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=45642

  Log:
    * st.c (st_foreach_check): chnage start point of search at check
      from top to current. [ruby-dev:48047] [Bug #9646]

  Modified files:
    trunk/ChangeLog
    trunk/st.c
    trunk/test/ruby/test_hash.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 45641)
+++ ChangeLog	(revision 45642)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Sun Apr 20 12:57:15 2014  Masaya Tarui  <tarui@r...>
+
+	* st.c (st_foreach_check): chnage start point of search at check
+	  from top to current. [ruby-dev:48047] [Bug #9646]
+
 Sun Apr 20 08:41:33 2014  Andrew DeMaria  <ademariad@g...>
 
 	* lib/mkmf.rb (link_command, libpathflag, create_makefile): prefer
Index: st.c
===================================================================
--- st.c	(revision 45641)
+++ st.c	(revision 45642)
@@ -342,9 +342,8 @@ find_entry(st_table *table, st_data_t ke https://github.com/ruby/ruby/blob/trunk/st.c#L342
 }
 
 static inline st_index_t
-find_packed_index(st_table *table, st_index_t hash_val, st_data_t key)
+find_packed_index_from(st_table *table, st_index_t hash_val, st_data_t key, st_index_t i)
 {
-    st_index_t i = 0;
     while (i < table->real_entries &&
 	   (PHASH(table, i) != hash_val || !EQUAL(table, key, PKEY(table, i)))) {
 	i++;
@@ -352,6 +351,12 @@ find_packed_index(st_table *table, st_in https://github.com/ruby/ruby/blob/trunk/st.c#L351
     return i;
 }
 
+static inline st_index_t
+find_packed_index(st_table *table, st_index_t hash_val, st_data_t key)
+{
+    return find_packed_index_from(table, hash_val, key, 0);
+}
+
 #define collision_check 0
 
 int
@@ -910,9 +915,10 @@ st_foreach_check(st_table *table, int (* https://github.com/ruby/ruby/blob/trunk/st.c#L915
 		if (PHASH(table, i) == 0 && PKEY(table, i) == never) {
 		    break;
 		}
-		i = find_packed_index(table, hash, key);
-		if (i == table->real_entries) {
-		    goto deleted;
+		i = find_packed_index_from(table, hash, key, i);
+		if (i >= table->real_entries) {
+		    i = find_packed_index(table, hash, key);
+		    if (i >= table->real_entries) goto deleted;
 		}
 		/* fall through */
 	      case ST_CONTINUE:
Index: test/ruby/test_hash.rb
===================================================================
--- test/ruby/test_hash.rb	(revision 45641)
+++ test/ruby/test_hash.rb	(revision 45642)
@@ -1088,6 +1088,17 @@ class TestHash < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_hash.rb#L1088
     assert_predicate(h.dup, :compare_by_identity?, bug8703)
   end
 
+  def test_same_key
+    bug9646 = '[ruby-dev:48047] [Bug #9646] Infinite loop at Hash#each'
+    h = @cls[a=[], 1]
+    a << 1
+    h[[]] = 2
+    a.clear
+    cnt = 0
+    r = h.each{ break nil if (cnt+=1) > 100 }
+    assert_not_nil(r,bug9646)
+  end
+
   class ObjWithHash
     def initialize(value, hash)
       @value = value

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

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