Skip to content

Commit c59b9d2

Browse files
authored
Merge pull request #197 from ruby/claude/deflate-params-retry-32f501
Count Zlib::Deflate#params output before warning about Z_BUF_ERROR
2 parents de62d8e + 784f279 commit c59b9d2

2 files changed

Lines changed: 32 additions & 1 deletion

File tree

‎ext/zlib/zlib.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2015,8 +2015,8 @@ rb_deflate_params(VALUE obj, VALUE v_level, VALUE v_strategy)
20152015
err = deflateParams(&z->stream, level, strategy);
20162016
filled = n - z->stream.avail_out;
20172017
while (err == Z_BUF_ERROR) {
2018-
rb_warning("deflateParams() returned Z_BUF_ERROR");
20192018
rb_str_set_len(z->buf, RSTRING_LEN(z->buf) + filled);
2019+
rb_warning("deflateParams() returned Z_BUF_ERROR");
20202020
zstream_expand_buffer(z);
20212021
n = z->stream.avail_out;
20222022
err = deflateParams(&z->stream, level, strategy);

‎test/zlib/test_zlib.rb‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,37 @@ def test_closed_p
238238
assert_equal(true, z.closed?)
239239
end
240240

241+
def test_params_with_raising_warning
242+
data = Random.new(0).bytes(16_500)
243+
z = Zlib::Deflate.new
244+
s = z.deflate(data)
245+
z.avail_out = 1
246+
stderr = Object.new
247+
def stderr.write(*) raise IOError end
248+
begin
249+
verbose, $VERBOSE = $VERBOSE, true
250+
orig_stderr, $stderr = $stderr, stderr
251+
assert_raise(IOError) {z.params(Zlib::BEST_SPEED, Zlib::FILTERED)}
252+
ensure
253+
$stderr = orig_stderr
254+
$VERBOSE = verbose
255+
end
256+
s << z.finish
257+
assert_equal(data, Zlib::Inflate.inflate(s))
258+
end
259+
260+
def test_params_with_block
261+
data = Random.new(0).bytes(16_500)
262+
z = Zlib::Deflate.new
263+
s = z.deflate(data)
264+
z.avail_out = 1
265+
chunks = []
266+
EnvUtil.suppress_warning {z.params(Zlib::BEST_SPEED, Zlib::FILTERED) {|c| chunks << c}}
267+
assert_not_empty(chunks)
268+
s << chunks.join << z.finish
269+
assert_equal(data, Zlib::Inflate.inflate(s))
270+
end
271+
241272
def test_params_with_buf_error_and_pending_output
242273
data = Random.new(0).bytes(1000)
243274
z = Zlib::Deflate.new(Zlib::DEFAULT_COMPRESSION, Zlib::MAX_WBITS, 1)

0 commit comments

Comments
 (0)