From ce94add7fbef17cbfa10de6a5554455542e1081c Mon Sep 17 00:00:00 2001 From: Peter Zhu Date: Tue, 2 Sep 2025 15:24:13 +0200 Subject: [PATCH] Fix global variable counter for alias If we alias two variable multiple times, for example like this: alias $foo $bar alias $foo $bar Then we will increment the counter for entry2->var->counter each time, which is not correct because we are not adding more references to entry2->var. This reports as a memory leak in RUBY_FREE_AT_EXIT because entry2->var->counter will never reach 0 and thus will never be freed. --- variable.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/variable.c b/variable.c index ef35cf39903dc6..b1782a01c85c93 100644 --- a/variable.c +++ b/variable.c @@ -1164,7 +1164,7 @@ rb_alias_variable(ID name1, ID name2) free_global_variable(var); } } - if (entry1) { + if (entry1->var != entry2->var) { entry2->var->counter++; entry1->var = entry2->var; }