summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Zhu <peter@peterzhu.ca>2025-10-08 17:08:20 -0400
committerPeter Zhu <peter@peterzhu.ca>2025-10-10 19:24:55 -0400
commit07b59eee6aa120537d7d72422327cc7b855e9400 (patch)
tree66cb59c0ef9f9badd9d7a7a78ec686c07e2ec8f4
parentd0d1246cd414655356e218e25a1c32666227e504 (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.c4
1 files changed, 3 insertions, 1 deletions
diff --git a/compile.c b/compile.c
index cb8554f0a1..717ec0a2ca 100644
--- a/compile.c
+++ b/compile.c
@@ -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) {