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

ruby-changes:33222

From: nobu <ko1@a...>
Date: Sun, 9 Mar 2014 13:51:49 +0900 (JST)
Subject: [ruby-changes:33222] nobu:r45301 (trunk): fiddle/function.c: fix memory leak on exception

nobu	2014-03-09 13:51:38 +0900 (Sun, 09 Mar 2014)

  New Revision: 45301

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

  Log:
    fiddle/function.c: fix memory leak on exception
    
    * ext/fiddle/function.c (function_call): fix memory leak when an
      exception occurs at argument conversion or the function call.

  Modified files:
    trunk/ChangeLog
    trunk/ext/fiddle/function.c
    trunk/test/fiddle/test_function.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 45300)
+++ ChangeLog	(revision 45301)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Sun Mar  9 13:51:16 2014  Nobuyoshi Nakada  <nobu@r...>
+
+	* ext/fiddle/function.c (function_call): fix memory leak when an
+	  exception occurs at argument conversion or the function call.
+
 Sun Mar  9 06:42:40 2014  Eric Wong  <e@8...>
 
 	* variable.c (struct global_variable): shrink by 8 bytes on 64-bit
Index: ext/fiddle/function.c
===================================================================
--- ext/fiddle/function.c	(revision 45300)
+++ ext/fiddle/function.c	(revision 45301)
@@ -137,6 +137,7 @@ function_call(int argc, VALUE argv[], VA https://github.com/ruby/ruby/blob/trunk/ext/fiddle/function.c#L137
     void **values;
     VALUE cfunc, types, cPointer;
     int i;
+    VALUE alloc_buffer = 0;
 
     cfunc    = rb_iv_get(self, "@ptr");
     types    = rb_iv_get(self, "@args");
@@ -159,8 +160,9 @@ function_call(int argc, VALUE argv[], VA https://github.com/ruby/ruby/blob/trunk/ext/fiddle/function.c#L160
 	}
     }
 
-    values = xcalloc((size_t)argc + 1, (size_t)sizeof(void *));
-    generic_args = xcalloc((size_t)argc, (size_t)sizeof(fiddle_generic));
+    generic_args = ALLOCV(alloc_buffer,
+	(size_t)(argc + 1) * sizeof(void *) + (size_t)argc * sizeof(fiddle_generic));
+    values = (void **)((char *)generic_args + (size_t)argc * sizeof(fiddle_generic));
 
     for (i = 0; i < argc; i++) {
 	VALUE type = RARRAY_PTR(types)[i];
@@ -187,8 +189,7 @@ function_call(int argc, VALUE argv[], VA https://github.com/ruby/ruby/blob/trunk/ext/fiddle/function.c#L189
     rb_funcall(mFiddle, rb_intern("win32_last_error="), 1, INT2NUM(errno));
 #endif
 
-    xfree(values);
-    xfree(generic_args);
+    ALLOCV_END(alloc_buffer);
 
     return GENERIC2VALUE(rb_iv_get(self, "@return_type"), retval);
 }
Index: test/fiddle/test_function.rb
===================================================================
--- test/fiddle/test_function.rb	(revision 45300)
+++ test/fiddle/test_function.rb	(revision 45301)
@@ -1,10 +1,13 @@ https://github.com/ruby/ruby/blob/trunk/test/fiddle/test_function.rb#L1
 begin
   require_relative 'helper'
+  require_relative '../ruby/envutil'
 rescue LoadError
 end
 
 module Fiddle
   class TestFunction < Fiddle::TestCase
+    include Test::Unit::Assertions
+
     def setup
       super
       Fiddle.last_error = nil
@@ -70,5 +73,11 @@ module Fiddle https://github.com/ruby/ruby/blob/trunk/test/fiddle/test_function.rb#L73
       assert_equal("123", buff)
       assert_equal("123", str.to_s)
     end
+
+    def test_no_memory_leak
+      prep = 'r = Fiddle::Function.new(Fiddle.dlopen(nil)["rb_obj_tainted"], [Fiddle::TYPE_UINTPTR_T], Fiddle::TYPE_UINTPTR_T); a = "a"'
+      code = 'begin r.call(a); rescue TypeError; end'
+      assert_no_memory_leak(%w[-W0 -rfiddle], "#{prep}\n1000.times{#{code}}", "10_000.times {#{code}}", limit: 1.2)
+    end
   end
 end if defined?(Fiddle)

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

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