1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

object.c: fix memory leak

* object.c (rb_obj_copy_ivar): allocate no memory for empty
  instance variables.  [ruby-core:64700] [Bug #10191]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@47372 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2014-09-03 07:56:09 +00:00
parent 8322ca8a21
commit b46b1e3f5a
3 changed files with 20 additions and 2 deletions

View file

@ -1,3 +1,8 @@
Wed Sep 3 16:56:07 2014 Nobuyoshi Nakada <nobu@ruby-lang.org>
* object.c (rb_obj_copy_ivar): allocate no memory for empty
instance variables. [ruby-core:64700] [Bug #10191]
Wed Sep 3 12:05:17 2014 Tanaka Akira <akr@fsij.org>
* process.c (retry_fork_async_signal_safe): Use vfork() if available.

View file

@ -270,8 +270,11 @@ rb_obj_copy_ivar(VALUE dest, VALUE obj)
}
else {
long len = ROBJECT(obj)->as.heap.numiv;
VALUE *ptr = ALLOC_N(VALUE, len);
MEMCPY(ptr, ROBJECT(obj)->as.heap.ivptr, VALUE, len);
VALUE *ptr = 0;
if (len > 0) {
ptr = ALLOC_N(VALUE, len);
MEMCPY(ptr, ROBJECT(obj)->as.heap.ivptr, VALUE, len);
}
ROBJECT(dest)->as.heap.ivptr = ptr;
ROBJECT(dest)->as.heap.numiv = len;
ROBJECT(dest)->as.heap.iv_index_tbl = ROBJECT(obj)->as.heap.iv_index_tbl;

View file

@ -811,4 +811,14 @@ class TestObject < Test::Unit::TestCase
assert_raise_with_message(TypeError, "can't convert Array into Integer") {Integer([42])}
assert_raise_with_message(TypeError, 'no implicit conversion of Array into Integer') {[].first([42])}
end
def test_copied_ivar_memory_leak
bug10191 = '[ruby-core:64700] [Bug #10191]'
assert_no_memory_leak([], <<-"end;", <<-"end;", bug10191, rss: true)
def (a = Object.new).set; @v = nil; end
num = 500_000
end;
num.times {a.clone.set}
end;
end
end