Skip to content

Commit

Permalink
Fix memory leak in Hash#compare_by_identity
Browse files Browse the repository at this point in the history
We didn't free the old ST before overwriting it which caused a leak.
Found with RUBY_FREE_ON_EXIT.

Co-authored-by: Peter Zhu <peter@peterzhu.ca>
  • Loading branch information
2 people authored and tenderlove committed Dec 13, 2023
1 parent cc86fa8 commit a3b48ac
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
1 change: 1 addition & 0 deletions hash.c
Expand Up @@ -4372,6 +4372,7 @@ rb_hash_compare_by_id(VALUE hash)

rb_hash_foreach(hash, rb_hash_rehash_i, (VALUE)tmp);

rb_hash_free(hash);
RHASH_ST_TABLE_SET(hash, identtable);
RHASH_ST_CLEAR(tmp);

Expand Down
8 changes: 8 additions & 0 deletions test/ruby/test_hash.rb
Expand Up @@ -1909,6 +1909,14 @@ def test_st_literal_memory_leak
end;
end

def test_compare_by_id_memory_leak
assert_no_memory_leak([], "", <<~RUBY, rss: true)
1_000_000.times do
{a: 1, b: 2, c: 3, d: 4, e: 5, f: 6, g: 7, h: 8}.compare_by_identity
end
RUBY
end

def test_try_convert
assert_equal({1=>2}, Hash.try_convert({1=>2}))
assert_equal(nil, Hash.try_convert("1=>2"))
Expand Down

0 comments on commit a3b48ac

Please sign in to comment.