aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNAKAMURA Usaku <usa@ruby-lang.org>2021-11-24 19:31:43 +0900
committerNAKAMURA Usaku <usa@ruby-lang.org>2021-11-24 19:31:43 +0900
commitd55426f800546cbc3b333ae7ab98c1893f710612 (patch)
tree50c11a748c866f15ababe694955d1eb2ea7fa60c
parent0fd9588395294ac97a1f6dc3a51d4f19e84a6ba9 (diff)
downloadruby-d55426f800546cbc3b333ae7ab98c1893f710612.tar.gz
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(-)
-rw-r--r--string.c2
-rw-r--r--test/ruby/test_string.rb10
-rw-r--r--version.h2
3 files changed, 12 insertions, 2 deletions
diff --git a/string.c b/string.c
index d6fd73115b..c6dec224f1 100644
--- a/string.c
+++ b/string.c
@@ -1594,7 +1594,7 @@ rb_str_init(int argc, VALUE *argv, VALUE str)
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 746471553d..b6cb0321c8 100644
--- a/test/ruby/test_string.rb
+++ b/test/ruby/test_string.rb
@@ -107,6 +107,16 @@ PREP
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 242f985511..81ae97bb31 100644
--- a/version.h
+++ b/version.h
@@ -2,7 +2,7 @@
# define RUBY_VERSION_MINOR RUBY_API_VERSION_MINOR
#define RUBY_VERSION_TEENY 5
#define RUBY_RELEASE_DATE RUBY_RELEASE_YEAR_STR"-"RUBY_RELEASE_MONTH_STR"-"RUBY_RELEASE_DAY_STR
-#define RUBY_PATCHLEVEL 196
+#define RUBY_PATCHLEVEL 197
#define RUBY_RELEASE_YEAR 2021
#define RUBY_RELEASE_MONTH 11