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

ruby-changes:67921

From: nagachika <ko1@a...>
Date: Sat, 11 Sep 2021 14:18:43 +0900 (JST)
Subject: [ruby-changes:67921] 650af7d29d (ruby_3_0): merge revision(s) 5d815542815fe8b939239750bba7f8f0b79c97d6: [Backport #18154]

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

From 650af7d29d98de6a3c2631e31edc6fbe435ece89 Mon Sep 17 00:00:00 2001
From: nagachika <nagachika@r...>
Date: Sat, 11 Sep 2021 14:00:44 +0900
Subject: merge revision(s) 5d815542815fe8b939239750bba7f8f0b79c97d6: [Backport
 #18154]

	[Bug #18154] Fix memory leak in String#initialize

	String#initialize can leak memory when called on a string that is marked
	with STR_NOFREE because it does not unset the STR_NOFREE flag.
	---
	 string.c                 |  2 +-
	 test/ruby/test_string.rb | 10 ++++++++++
	 2 files changed, 11 insertions(+), 1 deletion(-)
---
 string.c                 |  2 +-
 test/ruby/test_string.rb | 10 ++++++++++
 version.h                |  2 +-
 3 files changed, 12 insertions(+), 2 deletions(-)

diff --git a/string.c b/string.c
index ed10118..e8021cc 100644
--- a/string.c
+++ b/string.c
@@ -1730,7 +1730,7 @@ rb_str_init(int argc, VALUE *argv, VALUE str) https://github.com/ruby/ruby/blob/trunk/string.c#L1730
                 const size_t osize = RSTRING(str)->as.heap.len + TERM_LEN(str);
                 char *new_ptr = ALLOC_N(char, (size_t)capa + termlen);
                 memcpy(new_ptr, old_ptr, osize < size ? osize : size);
-                FL_UNSET_RAW(str, STR_SHARED);
+                FL_UNSET_RAW(str, STR_SHARED|STR_NOFREE);
                 RSTRING(str)->as.heap.ptr = new_ptr;
 	    }
 	    else if (STR_HEAP_SIZE(str) != (size_t)capa + termlen) {
diff --git a/test/ruby/test_string.rb b/test/ruby/test_string.rb
index 29ad98b..57e7dae 100644
--- a/test/ruby/test_string.rb
+++ b/test/ruby/test_string.rb
@@ -105,6 +105,16 @@ PREP https://github.com/ruby/ruby/blob/trunk/test/ruby/test_string.rb#L105
 CODE
   end
 
+  # Bug #18154
+  def test_initialize_nofree_memory_leak
+    assert_no_memory_leak([], <<-PREP, <<-CODE, rss: true)
+code = proc {0.to_s.__send__(:initialize, capacity: 10000)}
+1_000.times(&code)
+PREP
+100_000.times(&code)
+CODE
+  end
+
   def test_AREF # '[]'
     assert_equal("A",  S("AooBar")[0])
     assert_equal("B",  S("FooBaB")[-1])
diff --git a/version.h b/version.h
index ea49bd2..9380309 100644
--- a/version.h
+++ b/version.h
@@ -12,7 +12,7 @@ https://github.com/ruby/ruby/blob/trunk/version.h#L12
 # define RUBY_VERSION_MINOR RUBY_API_VERSION_MINOR
 #define RUBY_VERSION_TEENY 3
 #define RUBY_RELEASE_DATE RUBY_RELEASE_YEAR_STR"-"RUBY_RELEASE_MONTH_STR"-"RUBY_RELEASE_DAY_STR
-#define RUBY_PATCHLEVEL 132
+#define RUBY_PATCHLEVEL 133
 
 #define RUBY_RELEASE_YEAR 2021
 #define RUBY_RELEASE_MONTH 9
-- 
cgit v1.1


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

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