[前][次][番号順一覧][スレッド一覧]

ruby-changes:34476

From: nobu <ko1@a...>
Date: Thu, 26 Jun 2014 08:28:08 +0900 (JST)
Subject: [ruby-changes:34476] nobu:r46557 (trunk): hash.c: fix memory leak

nobu	2014-06-26 08:28:04 +0900 (Thu, 26 Jun 2014)

  New Revision: 46557

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=46557

  Log:
    hash.c: fix memory leak
    
    * 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]

  Modified files:
    trunk/ChangeLog
    trunk/hash.c
    trunk/test/ruby/test_env.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 46556)
+++ ChangeLog	(revision 46557)
@@ -1,3 +1,9 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Thu Jun 26 08:28:01 2014  Nobuyoshi Nakada  <nobu@r...>
+
+	* 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]
+
 Thu Jun 26 02:45:04 2014  Nobuyoshi Nakada  <nobu@r...>
 
 	* eval_error.c (error_print): put a newline after an anonymous
Index: hash.c
===================================================================
--- hash.c	(revision 46556)
+++ hash.c	(revision 46557)
@@ -3138,23 +3138,21 @@ static VALUE https://github.com/ruby/ruby/blob/trunk/hash.c#L3138
 env_select(VALUE ehash)
 {
     VALUE result;
-    char **env;
+    VALUE keys;
+    long i;
 
     RETURN_SIZED_ENUMERATOR(ehash, 0, 0, rb_env_size);
     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_AREF(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;
 }
Index: test/ruby/test_env.rb
===================================================================
--- test/ruby/test_env.rb	(revision 46556)
+++ test/ruby/test_env.rb	(revision 46557)
@@ -517,4 +517,20 @@ class TestEnv < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_env.rb#L517
       v = (ENV[k] = 'bar'*5000 rescue 'bar'*1500)
     end;
   end
+
+  def test_memory_leak_select
+    bug9978 = '[ruby-dev:48325] [Bug #9978]'
+    assert_no_memory_leak([], <<-'end;', "5_000.times {ENV.select {break}}", bug9978)
+      ENV.clear
+      k = 'FOO'
+      (ENV[k] = 'bar'*5000 rescue 'bar'*1500)
+    end;
+  end
+
+  def test_memory_crash_select
+    assert_normal_exit(<<-'end;')
+      1000.times {ENV["FOO#{i}"] = 'bar'}
+      ENV.select {ENV.clear}
+    end;
+  end
 end

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]