Skip to content

Commit 957c832

Browse files
committed
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.
1 parent 2f20dc5 commit 957c832

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

variable.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3533,14 +3533,23 @@ rb_const_remove(VALUE mod, ID id)
35333533
rb_check_frozen(mod);
35343534

35353535
ce = rb_const_lookup(mod, id);
3536-
if (!ce || !rb_id_table_delete(RCLASS_WRITABLE_CONST_TBL(mod), id)) {
3536+
3537+
if (!ce) {
35373538
if (rb_const_defined_at(mod, id)) {
35383539
rb_name_err_raise("cannot remove %2$s::%1$s", mod, ID2SYM(id));
35393540
}
35403541

35413542
undefined_constant(mod, ID2SYM(id));
35423543
}
35433544

3545+
VALUE writable_ce = 0;
3546+
if (rb_id_table_lookup(RCLASS_WRITABLE_CONST_TBL(mod), id, &writable_ce)) {
3547+
rb_id_table_delete(RCLASS_WRITABLE_CONST_TBL(mod), id);
3548+
if ((rb_const_entry_t *)writable_ce != ce) {
3549+
xfree((rb_const_entry_t *)writable_ce);
3550+
}
3551+
}
3552+
35443553
rb_const_warn_if_deprecated(ce, mod, id);
35453554
rb_clear_constant_cache_for_id(id);
35463555

0 commit comments

Comments
 (0)