Skip to content

Commit d828cf4

Browse files
authored
Merge pull request #164 from mame/zlib-gzfile-econv-leak
Close the encoding converter when a GzipFile is freed
2 parents 22d8ed8 + 715474c commit d828cf4

2 files changed

Lines changed: 13 additions & 0 deletions

File tree

‎ext/zlib/zlib.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2587,6 +2587,9 @@ gzfile_free(void *p)
25872587
}
25882588
zstream_finalize(z);
25892589
}
2590+
if (gz->ec) {
2591+
rb_econv_close(gz->ec);
2592+
}
25902593
xfree(gz);
25912594
}
25922595

‎test/zlib/test_zlib.rb‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1960,6 +1960,16 @@ def test_gunzip_no_memory_leak
19601960
};
19611961
end
19621962

1963+
def test_gzip_reader_encoding_conversion_no_memory_leak
1964+
assert_no_memory_leak(%W[-r#{LOADED_ZLIB} -rstringio], "#{<<~"{#"}", "#{<<~'};'}", rss: true)
1965+
d = Zlib.gzip("data")
1966+
opts = {external_encoding: "Shift_JIS", internal_encoding: "UTF-8"}
1967+
10_000.times {Zlib::GzipReader.new(StringIO.new(d), **opts).close}
1968+
{#
1969+
20_000.times {Zlib::GzipReader.new(StringIO.new(d), **opts).close}
1970+
};
1971+
end
1972+
19631973
# Test for signal interrupt bug: Z_BUF_ERROR with avail_out > 0
19641974
# This reproduces the issue where thread wakeup during GzipReader operations
19651975
# can cause Z_BUF_ERROR to be raised incorrectly

0 commit comments

Comments
 (0)