mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
Fix memory leak
* string.c (str_replace_shared_without_enc): free previous buffer before replaced. * parse.y (gettable): make sure in advance that the `__FILE__` object shares a fstring, to get rid of replacement with the fstring later. TODO: this hack may be needed in other places. [Bug #15916] Co-Authored-By: luke-gru (Luke Gruber) <luke.gru@gmail.com>
This commit is contained in:
parent
d4929f5185
commit
53e9908d8a
2 changed files with 8 additions and 1 deletions
2
parse.y
2
parse.y
|
@ -9748,7 +9748,7 @@ gettable(struct parser_params *p, ID id, const YYLTYPE *loc)
|
|||
if (NIL_P(file))
|
||||
file = rb_str_new(0, 0);
|
||||
else
|
||||
file = rb_str_dup(file);
|
||||
file = rb_str_dup(rb_fstring(file));
|
||||
node = NEW_STR(add_mark_object(p, file), loc);
|
||||
}
|
||||
return node;
|
||||
|
|
7
string.c
7
string.c
|
@ -1162,6 +1162,13 @@ str_replace_shared_without_enc(VALUE str2, VALUE str)
|
|||
}
|
||||
else {
|
||||
str = rb_str_new_frozen(str);
|
||||
if (!STR_EMBED_P(str2) && !FL_TEST_RAW(str2, STR_SHARED|STR_NOFREE)) {
|
||||
/* TODO: check if str2 is a shared root */
|
||||
char *ptr2 = STR_HEAP_PTR(str2);
|
||||
if (STR_HEAP_PTR(str) != ptr2) {
|
||||
ruby_sized_xfree(ptr2, STR_HEAP_SIZE(str2));
|
||||
}
|
||||
}
|
||||
FL_SET(str2, STR_NOEMBED);
|
||||
RSTRING_GETMEM(str, ptr, len);
|
||||
RSTRING(str2)->as.heap.len = len;
|
||||
|
|
Loading…
Reference in a new issue