Skip to content

Commit 1f052fd

Browse files
authored
Merge pull request #168 from ruby/claude/inflate-sync-in-progress-10a9a2
Raise InProgressError from Zlib::Inflate#sync during inflate
2 parents 22aeed0 + bbff886 commit 1f052fd

2 files changed

Lines changed: 22 additions & 2 deletions

File tree

‎ext/zlib/zlib.c‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1282,6 +1282,10 @@ zstream_sync(struct zstream *z, Bytef *src, rb_len_t len)
12821282
/* VALUE rest; */
12831283
int err;
12841284

1285+
if (z->flags & ZSTREAM_IN_PROGRESS) {
1286+
rb_raise(cInProgressError, "zlib stream is in progress");
1287+
}
1288+
12851289
if (!NIL_P(z->input)) {
12861290
err = zstream_inflate_sync(z, (Bytef*)RSTRING_PTR(z->input),
12871291
RSTRING_LEN(z->input));
@@ -2355,8 +2359,11 @@ rb_inflate_addstr(VALUE obj, VALUE src)
23552359
*
23562360
* Inputs +string+ into the end of input buffer and skips data until a full
23572361
* flush point can be found. If the point is found in the buffer, this method
2358-
* flushes the buffer and returns false. Otherwise it returns +true+ and the
2359-
* following data of full flush point is preserved in the buffer.
2362+
* returns +true+ and the following data of full flush point is preserved in
2363+
* the buffer. Otherwise it flushes the buffer and returns +false+.
2364+
*
2365+
* Raises a Zlib::InProgressError exception if called while the stream is
2366+
* being inflated.
23602367
*/
23612368
static VALUE
23622369
rb_inflate_sync(VALUE obj, VALUE src)

‎test/zlib/test_zlib.rb‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,19 @@ def test_sync
647647
assert_equal(true, z.sync("\0" * 100 + s))
648648
end
649649

650+
def test_sync_in_progress
651+
src = "x" * 100_000
652+
z = Zlib::Inflate.new
653+
inflated = "".dup
654+
z.inflate(Zlib.deflate(src)) do |chunk|
655+
if inflated.empty?
656+
assert_raise(Zlib::InProgressError) { z.sync("\0\0\xff\xff".b) }
657+
end
658+
inflated << chunk
659+
end
660+
assert_equal src, inflated
661+
end
662+
650663
def test_set_dictionary
651664
z = Zlib::Inflate.new
652665
assert_raise(Zlib::StreamError) { z.set_dictionary("foo") }

0 commit comments

Comments
 (0)