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

ruby-changes:57828

From: Nobuyoshi <ko1@a...>
Date: Fri, 20 Sep 2019 10:54:10 +0900 (JST)
Subject: [ruby-changes:57828] 422ae594d9 (master): Fixed memory leak

https://git.ruby-lang.org/ruby.git/commit/?id=422ae594d9

From 422ae594d97e803e829e65716e85483f5942e0c3 Mon Sep 17 00:00:00 2001
From: Nobuyoshi Nakada <nobu@r...>
Date: Fri, 20 Sep 2019 10:29:24 +0900
Subject: Fixed memory leak

* array.c (flatten): fix a memory leak in the case of an exception
  at conversion of an element to Array.

diff --git a/array.c b/array.c
index 55b408e..68f9321 100644
--- a/array.c
+++ b/array.c
@@ -5119,13 +5119,16 @@ static VALUE https://github.com/ruby/ruby/blob/trunk/array.c#L5119
 flatten(VALUE ary, int level, int *modified)
 {
     long i = 0;
-    VALUE stack, result, tmp, elt;
+    VALUE stack, result, tmp, elt, vmemo;
     st_table *memo;
     st_data_t id;
 
     stack = ary_new(0, ARY_DEFAULT_SIZE);
     result = ary_new(0, RARRAY_LEN(ary));
+    vmemo = rb_hash_new();
+    RBASIC_CLEAR_CLASS(vmemo);
     memo = st_init_numtable();
+    rb_hash_st_table_set(vmemo, memo);
     st_insert(memo, (st_data_t)ary, (st_data_t)Qtrue);
     *modified = 0;
 
@@ -5138,6 +5141,8 @@ flatten(VALUE ary, int level, int *modified) https://github.com/ruby/ruby/blob/trunk/array.c#L5141
 	    }
 	    tmp = rb_check_array_type(elt);
 	    if (RBASIC(result)->klass) {
+                RB_GC_GUARD(vmemo);
+                st_clear(memo);
 		rb_raise(rb_eRuntimeError, "flatten reentered");
 	    }
 	    if (NIL_P(tmp)) {
@@ -5147,7 +5152,7 @@ flatten(VALUE ary, int level, int *modified) https://github.com/ruby/ruby/blob/trunk/array.c#L5152
 		*modified = 1;
 		id = (st_data_t)tmp;
 		if (st_lookup(memo, id, 0)) {
-		    st_free_table(memo);
+                    st_clear(memo);
 		    rb_raise(rb_eArgError, "tried to flatten recursive array");
 		}
 		st_insert(memo, id, (st_data_t)Qtrue);
@@ -5167,7 +5172,7 @@ flatten(VALUE ary, int level, int *modified) https://github.com/ruby/ruby/blob/trunk/array.c#L5172
 	ary = rb_ary_pop(stack);
     }
 
-    st_free_table(memo);
+    st_clear(memo);
 
     RBASIC_SET_CLASS(result, rb_obj_class(ary));
     return result;
-- 
cgit v0.10.2


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

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