From 2eb7847c42c50943be31942dac0638a45b61d4e8 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Sun, 16 Mar 2014 23:14:38 +0000 Subject: [PATCH] variable.c: avoid memory leak on const redefinition * variable.c (rb_const_set): delete existing entry on redefinition * test/ruby/test_const.rb (test_redefinition): test for leak --- test/ruby/test_const.rb | 8 ++++++++ variable.c | 2 ++ 2 files changed, 10 insertions(+) diff --git a/test/ruby/test_const.rb b/test/ruby/test_const.rb index dab45b7..c4a4d93 100644 --- a/test/ruby/test_const.rb +++ b/test/ruby/test_const.rb @@ -1,5 +1,6 @@ # -*- coding: us-ascii -*- require 'test/unit' +require_relative 'envutil' class TestConst < Test::Unit::TestCase TEST1 = 1 @@ -54,5 +55,12 @@ def test_redefinition #{__FILE__}:#{__LINE__-1}: warning: already initialized constant #{c}::X #{__FILE__}:#{__LINE__-3}: warning: previous definition of X was here WARNING + code = <<-PRE +olderr = $stderr.dup +$stderr.reopen(File::NULL, "wb") +350000.times { FOO = :BAR } +$stderr.reopen(olderr) +PRE + assert_no_memory_leak([], '', code, 'redefined constant') end end diff --git a/variable.c b/variable.c index c97c210..079f862 100644 --- a/variable.c +++ b/variable.c @@ -2203,6 +2203,8 @@ rb_const_set(VALUE klass, ID id, VALUE val) rb_compile_warn(RSTRING_PTR(ce->file), ce->line, "previous definition of %"PRIsVALUE" was here", name); } + st_delete(RCLASS_CONST_TBL(klass), &id, 0); + xfree(ce); } } } -- 1.9.rc1