diff options
| author | Peter Zhu <peter@peterzhu.ca> | 2025-10-08 17:08:20 -0400 |
|---|---|---|
| committer | Peter Zhu <peter@peterzhu.ca> | 2025-10-10 19:24:55 -0400 |
| commit | 07b59eee6aa120537d7d72422327cc7b855e9400 (patch) | |
| tree | 66cb59c0ef9f9badd9d7a7a78ec686c07e2ec8f4 | |
| parent | d0d1246cd414655356e218e25a1c32666227e504 (diff) | |
Fix memory leak when load_from_binary raises
ibf_load_code will leak memory allocated for the code if an exception is
raised. The following script reproduces the leak:
bin = RubyVM::InstructionSequence.of(1.method(:abs)).to_binary
10.times do
100_000.times do
RubyVM::InstructionSequence.load_from_binary(bin)
rescue ArgumentError
end
puts `ps -o rss= -p #{$$}`
end
Before:
18004
23380
28756
34260
39892
45396
50772
55892
61012
66132
After:
12536
12920
13304
13688
14072
14456
14840
15352
15608
15864
| -rw-r--r-- | compile.c | 4 |
1 files changed, 3 insertions, 1 deletions
@@ -12926,6 +12926,9 @@ ibf_load_code(const struct ibf_load *load, rb_iseq_t *iseq, ibf_offset_t bytecod struct rb_call_data *cd_entries = load_body->call_data; int ic_index = 0; + load_body->iseq_encoded = code; + load_body->iseq_size = 0; + iseq_bits_t * mark_offset_bits; iseq_bits_t tmp[1] = {0}; @@ -13057,7 +13060,6 @@ ibf_load_code(const struct ibf_load *load, rb_iseq_t *iseq, ibf_offset_t bytecod } } - load_body->iseq_encoded = code; load_body->iseq_size = code_index; if (ISEQ_MBITS_BUFLEN(load_body->iseq_size) == 1) { |
