Skip to content

Commit f0d8a0a

Browse files
committed
Fix memory leak in parser when loading non-ASCII file
When loading a non-ASCII compatible file, an error is raised which causes memory leak. For example: require "tempfile" Tempfile.create do |f| f.write("# -*- coding: UTF-16BE -*-") f.flush 10.times do 20_000.times do begin load(f.path) rescue end end puts `ps -o rss= -p #{$$}` end end Before: 33904 49072 64528 79216 94576 109504 124768 139536 154928 170256 After: 19568 21296 21664 21728 22192 22256 22416 22272 22272 22272
1 parent 57b11be commit f0d8a0a

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

parse.y

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9417,6 +9417,10 @@ parser_set_encode(struct parser_params *p, const char *name)
94179417
rb_ary_unshift(excargs[2], rb_sprintf("%"PRIsVALUE":%d", p->ruby_sourcefile_string, p->ruby_sourceline));
94189418
VALUE exc = rb_make_exception(3, excargs);
94199419
ruby_show_error_line(p, exc, &(YYLTYPE)RUBY_INIT_YYLLOC(), p->ruby_sourceline, p->lex.lastline);
9420+
9421+
rb_ast_free(p->ast);
9422+
p->ast = NULL;
9423+
94209424
rb_exc_raise(exc);
94219425
}
94229426
enc = rb_enc_from_index(idx);

0 commit comments

Comments
 (0)