diff --git a/ext/zlib/zlib.c b/ext/zlib/zlib.c index e279bb9..f8e852c 100644 --- a/ext/zlib/zlib.c +++ b/ext/zlib/zlib.c @@ -959,6 +959,13 @@ zstream_reset_input(struct zstream *z) } } +static void +zstream_clear_next_in(struct zstream *z) +{ + z->stream.next_in = Z_NULL; + z->stream.avail_in = 0; +} + static void zstream_passthrough_input(struct zstream *z) { @@ -1146,6 +1153,12 @@ zstream_run_func(struct zstream_run_args *args) return err; } +static VALUE +zstream_run_func_protect(VALUE args) +{ + return (VALUE)zstream_run_func((struct zstream_run_args *)args); +} + static VALUE zstream_run_try(VALUE value_arg) { @@ -1155,7 +1168,7 @@ zstream_run_try(VALUE value_arg) rb_len_t len = args->len; int flush = args->flush; - int err; + int err, state; VALUE old_input = Qnil; /* Cannot start zstream while it is in progress. */ @@ -1191,7 +1204,13 @@ zstream_run_try(VALUE value_arg) } loop: - err = zstream_run_func(args); + err = (int)rb_protect(zstream_run_func_protect, (VALUE)args, &state); + if (state) { + zstream_reset_input(z); + zstream_append_input(z, z->stream.next_in, z->stream.avail_in + args->rest_in); + zstream_clear_next_in(z); + rb_jump_tag(state); + } /* retry if no exception is thrown */ if (err == Z_OK && args->interrupt) { @@ -1231,8 +1250,7 @@ zstream_run_try(VALUE value_arg) } } } - z->stream.next_in = Z_NULL; - z->stream.avail_in = 0; + zstream_clear_next_in(z); raise_zlib_error(err, z->stream.msg); } @@ -1240,9 +1258,7 @@ zstream_run_try(VALUE value_arg) if (!NIL_P(old_input)) { rb_str_resize(old_input, 0); } - /* the rest is in z->input now; do not leave next_in in old_input */ - z->stream.next_in = Z_NULL; - z->stream.avail_in = 0; + zstream_clear_next_in(z); return Qnil; } @@ -1289,8 +1305,6 @@ zstream_inflate_sync(struct zstream *z, Bytef *src, rb_len_t len) len -= z->stream.avail_in; err = inflateSync(&z->stream); } while (err == Z_DATA_ERROR && len > 0); - /* do_inflate() looks at avail_in for the input left after the sync point */ - z->stream.avail_in = MAX_UINT(z->stream.avail_in + len); return err; } @@ -1311,9 +1325,11 @@ zstream_sync(struct zstream *z, Bytef *src, rb_len_t len) if (err == Z_OK) { zstream_discard_input(z, z->stream.next_in - (Bytef*)RSTRING_PTR(z->input)); + zstream_clear_next_in(z); zstream_append_input(z, src, len); return Qtrue; } + zstream_clear_next_in(z); zstream_reset_input(z); if (err != Z_DATA_ERROR) { /* rest = rb_str_new((char*)z->stream.next_in, z->stream.avail_in); */ @@ -1326,8 +1342,10 @@ zstream_sync(struct zstream *z, Bytef *src, rb_len_t len) err = zstream_inflate_sync(z, src, len); if (err == Z_OK) { zstream_append_input(z, z->stream.next_in, src + len - z->stream.next_in); + zstream_clear_next_in(z); return Qtrue; } + zstream_clear_next_in(z); if (err != Z_DATA_ERROR) { /* rest = rb_str_new((char*)z->stream.next_in, z->stream.avail_in); */ raise_zlib_error(err, z->stream.msg); diff --git a/test/zlib/test_zlib.rb b/test/zlib/test_zlib.rb index afe9529..7dc0682 100644 --- a/test/zlib/test_zlib.rb +++ b/test/zlib/test_zlib.rb @@ -227,6 +227,27 @@ def test_closed_p assert_equal(true, z.closed?) end + def test_params_after_interrupted_deflate + assert_separately(%w[-rzlib], "#{<<~"begin;"}\n#{<<~'end;'}", timeout: 60) + begin; + data = Random.new(0).bytes(8 << 20) + z = nil + [0.001, 0.005, 0.01, 0.02, 0.05, 0.1].cycle(3) do |wait| + z = Zlib::Deflate.new(1) + th = Thread.new { z.deflate(data) rescue nil } + sleep wait + th.raise(RuntimeError) + th.join + break if z.total_in > 0 && z.total_in < data.bytesize + z = nil + end + omit "deflate was not interrupted in the middle" unless z + GC.start + EnvUtil.suppress_warning { z.params(9, Zlib::HUFFMAN_ONLY) } + assert_kind_of(String, z.finish) + end; + end + def test_params_with_buf_error_and_pending_output data = Random.new(0).bytes(1000) z = Zlib::Deflate.new(Zlib::DEFAULT_COMPRESSION, Zlib::MAX_WBITS, 1)