diff options
author | Peter Zhu <[email protected]> | 2024-11-08 14:33:48 -0500 |
---|---|---|
committer | Peter Zhu <[email protected]> | 2024-11-08 15:43:41 -0500 |
commit | 51ffef281996727c60571771cd07c1459ba58cd2 (patch) | |
tree | a6b6706cb5ed5324fadf271d49ecd0a336d9a53b /load.c | |
parent | 72550d269ea89cd0bfcede7ad01a7c70ed01ba06 (diff) |
Fix memory leak in prism when syntax error in iseq compilation
If there's a syntax error during iseq compilation then prism would leak
memory because it would not free the pm_parse_result_t.
This commit changes pm_iseq_new_with_opt to have a rb_protect to catch
when an error is raised, and return NULL and set error_state to a value
that can be raised by calling rb_jump_tag after memory has been freed.
For example:
10.times do
10_000.times do
eval("/[/=~s")
rescue SyntaxError
end
puts `ps -o rss= -p #{$$}`
end
Before:
39280
68736
99232
128864
158896
188208
217344
246304
275376
304592
After:
12192
13200
14256
14848
16000
16000
16000
16064
17232
17952
Notes
Notes:
Merged: https://github.com/ruby/ruby/pull/12036
Diffstat (limited to 'load.c')
-rw-r--r-- | load.c | 9 |
1 files changed, 8 insertions, 1 deletions
@@ -752,8 +752,15 @@ load_iseq_eval(rb_execution_context_t *ec, VALUE fname) VALUE error = pm_load_parse_file(&result, fname, NULL); if (error == Qnil) { - iseq = pm_iseq_new_top(&result.node, rb_fstring_lit("<top (required)>"), fname, realpath_internal_cached(realpath_map, fname), NULL); + int error_state; + iseq = pm_iseq_new_top(&result.node, rb_fstring_lit("<top (required)>"), fname, realpath_internal_cached(realpath_map, fname), NULL, &error_state); + pm_parse_result_free(&result); + + if (error_state) { + RUBY_ASSERT(iseq == NULL); + rb_jump_tag(error_state); + } } else { rb_vm_pop_frame(ec); |