ruby-changes:35283
From: usa <ko1@a...>
Date: Wed, 3 Sep 2014 12:43:26 +0900 (JST)
Subject: [ruby-changes:35283] usa:r47365 (ruby_2_0_0): merge revision(s) 46550, 46557, 46565, 46570, 46585, 46595, 46822: [Backport #9977] [Backport #9978] [Backport #9983]
usa 2014-09-03 12:43:19 +0900 (Wed, 03 Sep 2014) New Revision: 47365 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=47365 Log: merge revision(s) 46550,46557,46565,46570,46585,46595,46822: [Backport #9977] [Backport #9978] [Backport #9983] * hash.c (ruby_setenv): fix memory leak on Windows, free environment strings block after check for the size. [ruby-dev:48323] [Bug #9977] * hash.c (env_select): fix memory leak and crash on Windows, make keys array first instead of iterating on envrion directly. [ruby-dev:48325] [Bug #9978] keys array first instead of iterating on environ directly. * hash.c (env_shift): fix memory leak on Windows, free environment strings block always. [ruby-dev:48332] [Bug #9983] Modified directories: branches/ruby_2_0_0/ Modified files: branches/ruby_2_0_0/ChangeLog branches/ruby_2_0_0/hash.c branches/ruby_2_0_0/test/ruby/test_env.rb branches/ruby_2_0_0/version.h Index: ruby_2_0_0/ChangeLog =================================================================== --- ruby_2_0_0/ChangeLog (revision 47364) +++ ruby_2_0_0/ChangeLog (revision 47365) @@ -1,3 +1,20 @@ https://github.com/ruby/ruby/blob/trunk/ruby_2_0_0/ChangeLog#L1 +Wed Sep 3 12:41:35 2014 Nobuyoshi Nakada <nobu@r...> + + * hash.c (env_shift): fix memory leak on Windows, free environment + strings block always. [ruby-dev:48332] [Bug #9983] + +Wed Sep 3 12:41:35 2014 Nobuyoshi Nakada <nobu@r...> + + * hash.c (env_select): fix memory leak and crash on Windows, make + keys array first instead of iterating on environ directly. + [ruby-dev:48325] [Bug #9978] + +Wed Sep 3 12:41:35 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 Sep 3 12:24:39 2014 Nobuyoshi Nakada <nobu@r...> * re.c (match_aref, rb_reg_regsub): consider encoding of captured Index: ruby_2_0_0/hash.c =================================================================== --- ruby_2_0_0/hash.c (revision 47364) +++ ruby_2_0_0/hash.c (revision 47365) @@ -2416,9 +2416,12 @@ ruby_setenv(const char *name, const char https://github.com/ruby/ruby/blob/trunk/ruby_2_0_0/hash.c#L2416 rb_sys_fail("ruby_setenv"); } 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); @@ -2806,24 +2809,22 @@ static VALUE https://github.com/ruby/ruby/blob/trunk/ruby_2_0_0/hash.c#L2809 env_select(VALUE ehash) { VALUE result; - char **env; + VALUE keys; + long i; RETURN_SIZED_ENUMERATOR(ehash, 0, 0, rb_env_size); rb_secure(4); result = rb_hash_new(); - env = GET_ENVIRON(environ); - while (*env) { - char *s = strchr(*env, '='); - if (s) { - VALUE k = env_str_new(*env, s-*env); - VALUE v = env_str_new2(s+1); - if (RTEST(rb_yield_values(2, k, v))) { - rb_hash_aset(result, k, v); + keys = env_keys(); + for (i = 0; i < RARRAY_LEN(keys); ++i) { + VALUE key = RARRAY_PTR(keys)[i]; + VALUE val = rb_f_getenv(Qnil, key); + if (!NIL_P(val)) { + if (RTEST(rb_yield_values(2, key, val))) { + rb_hash_aset(result, key, val); } } - env++; } - FREE_ENVIRON(environ); return result; } @@ -3237,6 +3238,7 @@ static VALUE https://github.com/ruby/ruby/blob/trunk/ruby_2_0_0/hash.c#L3238 env_shift(void) { char **env; + VALUE result = Qnil; rb_secure(4); env = GET_ENVIRON(environ); @@ -3246,11 +3248,11 @@ env_shift(void) https://github.com/ruby/ruby/blob/trunk/ruby_2_0_0/hash.c#L3248 VALUE key = env_str_new(*env, s-*env); VALUE val = env_str_new2(getenv(RSTRING_PTR(key))); env_delete(Qnil, key); - return rb_assoc_new(key, val); + result = rb_assoc_new(key, val); } } FREE_ENVIRON(environ); - return Qnil; + return result; } /* Index: ruby_2_0_0/version.h =================================================================== --- ruby_2_0_0/version.h (revision 47364) +++ ruby_2_0_0/version.h (revision 47365) @@ -1,6 +1,6 @@ https://github.com/ruby/ruby/blob/trunk/ruby_2_0_0/version.h#L1 #define RUBY_VERSION "2.0.0" #define RUBY_RELEASE_DATE "2014-09-03" -#define RUBY_PATCHLEVEL 543 +#define RUBY_PATCHLEVEL 544 #define RUBY_RELEASE_YEAR 2014 #define RUBY_RELEASE_MONTH 9 Index: ruby_2_0_0/test/ruby/test_env.rb =================================================================== --- ruby_2_0_0/test/ruby/test_env.rb (revision 47364) +++ ruby_2_0_0/test/ruby/test_env.rb (revision 47365) @@ -1,4 +1,5 @@ https://github.com/ruby/ruby/blob/trunk/ruby_2_0_0/test/ruby/test_env.rb#L1 require 'test/unit' +require_relative 'envutil' class TestEnv < Test::Unit::TestCase IGNORE_CASE = /bccwin|mswin|mingw/ =~ RUBY_PLATFORM @@ -405,4 +406,46 @@ class TestEnv < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/ruby_2_0_0/test/ruby/test_env.rb#L406 } end end + + if RUBY_PLATFORM =~ /bccwin|mswin|mingw/ + def test_memory_leak_aset + bug9977 = '[ruby-dev:48323] [Bug #9977]' + assert_no_memory_leak([], <<-'end;', "5_000.times(&doit)", bug9977, limit: 2.0) + ENV.clear + k = 'FOO' + v = (ENV[k] = 'bar'*5000 rescue 'bar'*1500) + doit = proc {ENV[k] = v} + 500.times(&doit) + end; + end + + def test_memory_leak_select + bug9978 = '[ruby-dev:48325] [Bug #9978]' + assert_no_memory_leak([], <<-'end;', "5_000.times(&doit)", bug9978, limit: 2.0) + ENV.clear + k = 'FOO' + (ENV[k] = 'bar'*5000 rescue 'bar'*1500) + doit = proc {ENV.select {break}} + 500.times(&doit) + end; + end + + def test_memory_crash_select + assert_normal_exit(<<-'end;') + 1000.times {ENV["FOO#{i}"] = 'bar'} + ENV.select {ENV.clear} + end; + end + + def test_memory_leak_shift + bug9983 = '[ruby-dev:48332] [Bug #9983]' + assert_no_memory_leak([], <<-'end;', "5_000.times(&doit)", bug9983, limit: 2.0) + ENV.clear + k = 'FOO' + v = (ENV[k] = 'bar'*5000 rescue 'bar'*1500) + doit = proc {ENV[k] = v; ENV.shift} + 500.times(&doit) + end; + end + end end Property changes on: ruby_2_0_0 ___________________________________________________________________ Modified: svn:mergeinfo Merged /trunk:r46550,46557,46565,46570,46585,46595,46822 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/