1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

merge revision(s) 5d81554281: [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(-)
This commit is contained in:
nagachika 2021-09-11 14:00:44 +09:00
parent 13f64b65e0
commit 650af7d29d
3 changed files with 12 additions and 2 deletions

View file

@ -1730,7 +1730,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) {

View file

@ -105,6 +105,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])

View file

@ -12,7 +12,7 @@
# 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