summaryrefslogtreecommitdiff
path: root/variable.c
diff options
context:
space:
mode:
authorPeter Zhu <peter@peterzhu.ca>2025-10-18 14:32:25 -0400
committerPeter Zhu <peter@peterzhu.ca>2025-10-19 10:22:25 -0400
commit957c832db137e67289e93dfd9fd9e915b1f2fc87 (patch)
tree0915ebdd5829b108533cb22a81ea382b38f6f580 /variable.c
parent2f20dc5dc5806cf1a836420e0216b814d0c6252a (diff)
Fix memory leak in rb_const_remove when using namespace
We need to free the rb_const_entry_t we remove from the RCLASS_WRITABLE_CONST_TBL otherwise it will leak memory.
Diffstat (limited to 'variable.c')
-rw-r--r--variable.c11
1 files changed, 10 insertions, 1 deletions
diff --git a/variable.c b/variable.c
index c8565047a4..5fc98fb087 100644
--- a/variable.c
+++ b/variable.c
@@ -3533,7 +3533,8 @@ rb_const_remove(VALUE mod, ID id)
rb_check_frozen(mod);
ce = rb_const_lookup(mod, id);
- if (!ce || !rb_id_table_delete(RCLASS_WRITABLE_CONST_TBL(mod), id)) {
+
+ if (!ce) {
if (rb_const_defined_at(mod, id)) {
rb_name_err_raise("cannot remove %2$s::%1$s", mod, ID2SYM(id));
}
@@ -3541,6 +3542,14 @@ rb_const_remove(VALUE mod, ID id)
undefined_constant(mod, ID2SYM(id));
}
+ VALUE writable_ce = 0;
+ if (rb_id_table_lookup(RCLASS_WRITABLE_CONST_TBL(mod), id, &writable_ce)) {
+ rb_id_table_delete(RCLASS_WRITABLE_CONST_TBL(mod), id);
+ if ((rb_const_entry_t *)writable_ce != ce) {
+ xfree((rb_const_entry_t *)writable_ce);
+ }
+ }
+
rb_const_warn_if_deprecated(ce, mod, id);
rb_clear_constant_cache_for_id(id);