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

ruby-changes:60031

From: Nobuyoshi <ko1@a...>
Date: Thu, 13 Feb 2020 12:47:12 +0900 (JST)
Subject: [ruby-changes:60031] fce667ed08 (master): Get rid of warnings/exceptions at cleanup

https://git.ruby-lang.org/ruby.git/commit/?id=fce667ed08

From fce667ed08f25fa7ce43c9b07be170f341a04c4e Mon Sep 17 00:00:00 2001
From: Nobuyoshi Nakada <nobu@r...>
Date: Thu, 13 Feb 2020 09:34:49 +0900
Subject: Get rid of warnings/exceptions at cleanup

After the encoding index instance variable is removed when all
instance variables are removed in `obj_free`, then `rb_str_free`
causes uninitialized instance variable warning and nil-to-integer
conversion exception.  Both cases result in object allocation
during GC, and crashes.

diff --git a/encoding.c b/encoding.c
index f2e67ff..e713b0a 100644
--- a/encoding.c
+++ b/encoding.c
@@ -779,8 +779,18 @@ enc_get_index_str(VALUE str) https://github.com/ruby/ruby/blob/trunk/encoding.c#L779
     if (i == ENCODING_INLINE_MAX) {
 	VALUE iv;
 
+#if 0
 	iv = rb_ivar_get(str, rb_id_encoding());
 	i = NUM2INT(iv);
+#else
+        /*
+         * Tentatively, assume ASCII-8BIT, if encoding index instance
+         * variable is not found.  This can happen when freeing after
+         * all instance variables are removed in `obj_free`.
+         */
+        iv = rb_attr_get(str, rb_id_encoding());
+        i = NIL_P(iv) ? ENCINDEX_ASCII : NUM2INT(iv);
+#endif
     }
     return i;
 }
diff --git a/string.c b/string.c
index 3cfe3ad..28a1b29 100644
--- a/string.c
+++ b/string.c
@@ -181,6 +181,7 @@ VALUE rb_cSymbol; https://github.com/ruby/ruby/blob/trunk/string.c#L181
 
 #define STR_HEAP_PTR(str)  (RSTRING(str)->as.heap.ptr)
 #define STR_HEAP_SIZE(str) ((size_t)RSTRING(str)->as.heap.aux.capa + TERM_LEN(str))
+/* TODO: include the terminator size in capa. */
 
 #define STR_ENC_GET(str) get_encoding(str)
 
diff --git a/test/ruby/test_encoding.rb b/test/ruby/test_encoding.rb
index 80bed39..6fc5c48 100644
--- a/test/ruby/test_encoding.rb
+++ b/test/ruby/test_encoding.rb
@@ -76,6 +76,9 @@ class TestEncoding < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_encoding.rb#L76
       assert_equal("0", format % 0)
       assert_equal(e, format.dup.encoding)
       assert_equal(e, (format*1).encoding)
+
+      assert_equal(e, (("x"*30).force_encoding(e)*1).encoding)
+      GC.start
     end;
   end
 
-- 
cgit v0.10.2


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

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