ruby-changes:34469
From: nobu <ko1@a...>
Date: Wed, 25 Jun 2014 22:31:39 +0900 (JST)
Subject: [ruby-changes:34469] nobu:r46550 (trunk): hash.c: fix memory leak
nobu 2014-06-25 22:31:34 +0900 (Wed, 25 Jun 2014) New Revision: 46550 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=46550 Log: hash.c: fix memory leak * hash.c (ruby_setenv): fix memory leak on Windows, free environment strings block after check for the size. [ruby-dev:48323] [Bug #9977] Modified files: trunk/ChangeLog trunk/hash.c trunk/test/ruby/test_env.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 46549) +++ ChangeLog (revision 46550) @@ -1,3 +1,9 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Wed Jun 25 22:31:32 2014 Nobuyoshi Nakada <nobu@r...> + + * hash.c (ruby_setenv): fix memory leak on Windows, free + environment strings block after check for the size. + [ruby-dev:48323] [Bug #9977] + Wed Jun 25 15:44:12 2014 Eric Wong <e@8...> * ccan/container_of/container_of.h (container_off_var): Index: hash.c =================================================================== --- hash.c (revision 46549) +++ hash.c (revision 46550) @@ -2750,9 +2750,12 @@ ruby_setenv(const char *name, const char https://github.com/ruby/ruby/blob/trunk/hash.c#L2750 int failed = 0; check_envname(name); if (value) { - const char* p = GetEnvironmentStringsA(); + char* p = GetEnvironmentStringsA(); + size_t n; if (!p) goto fail; /* never happen */ - if (strlen(name) + 2 + strlen(value) + getenvsize(p) >= getenvblocksize()) { + n = strlen(name) + 2 + strlen(value) + getenvsize(p); + FreeEnvironmentStringsA(p); + if (n >= getenvblocksize()) { goto fail; /* 2 for '=' & '\0' */ } buf = rb_sprintf("%s=%s", name, value); Index: test/ruby/test_env.rb =================================================================== --- test/ruby/test_env.rb (revision 46549) +++ test/ruby/test_env.rb (revision 46550) @@ -1,4 +1,5 @@ https://github.com/ruby/ruby/blob/trunk/test/ruby/test_env.rb#L1 require 'test/unit' +require_relative 'envutil' class TestEnv < Test::Unit::TestCase IGNORE_CASE = /bccwin|mswin|mingw/ =~ RUBY_PLATFORM @@ -507,4 +508,13 @@ class TestEnv < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_env.rb#L508 end.call end end + + def test_memory_leak_aset + bug9977 = '[ruby-dev:48323] [Bug #9977]' + assert_no_memory_leak([], <<-'end;', "5_000.times {ENV[k] = v}", bug9977) + ENV.clear + k = 'FOO' + v = (ENV[k] = 'bar'*5000 rescue 'bar'*1500) + end; + end end -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/