Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 41 additions & 8 deletions ext/zlib/zlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ static VALUE rb_inflate_addstr(VALUE, VALUE);
static VALUE rb_inflate_sync(VALUE, VALUE);
static VALUE rb_inflate_sync_point_p(VALUE);
static VALUE rb_inflate_set_dictionary(VALUE, VALUE);
static void inflate_set_dictionary(struct zstream*, VALUE);

#if GZIP_SUPPORT
struct gzfile;
Expand Down Expand Up @@ -1177,6 +1178,14 @@ zstream_run_func(struct zstream_run_args *args)
return err;
}

static void
zstream_check_not_in_progress(struct zstream *z)
{
if (z->flags & ZSTREAM_IN_PROGRESS) {
rb_raise(cInProgressError, "zlib stream is in progress");
}
}

static VALUE
zstream_run_try(VALUE value_arg)
{
Expand Down Expand Up @@ -1256,7 +1265,7 @@ zstream_run_try(VALUE value_arg)
VALUE dicts = rb_ivar_get(self, id_dictionaries);
VALUE dict = rb_hash_aref(dicts, rb_uint2inum(z->stream.adler));
if (!NIL_P(dict)) {
rb_inflate_set_dictionary(self, dict);
inflate_set_dictionary(z, dict);
if (z->stream.avail_in > 0) goto loop;
/* the dictionary id was the last byte available; inflate()
* would only report Z_BUF_ERROR now */
Expand Down Expand Up @@ -1309,9 +1318,7 @@ zstream_run(struct zstream *z, Bytef *src, rb_len_t len, int flush)
};

/* Cannot start zstream while it is in progress. */
if (z->flags & ZSTREAM_IN_PROGRESS) {
rb_raise(cInProgressError, "zlib stream is in progress");
}
zstream_check_not_in_progress(z);

rb_ensure(zstream_run_try, (VALUE)&args, zstream_run_ensure, (VALUE)&args);
if (args.jump_state)
Expand Down Expand Up @@ -1516,7 +1523,10 @@ get_zstream(VALUE obj)
static VALUE
rb_zstream_end(VALUE obj)
{
zstream_end(get_zstream(obj));
struct zstream *z = get_zstream(obj);

zstream_check_not_in_progress(z);
zstream_end(z);
return Qnil;
}

Expand All @@ -1527,7 +1537,10 @@ rb_zstream_end(VALUE obj)
static VALUE
rb_zstream_reset(VALUE obj)
{
zstream_reset(get_zstream(obj));
struct zstream *z = get_zstream(obj);

zstream_check_not_in_progress(z);
zstream_reset(z);
return Qnil;
}

Expand Down Expand Up @@ -1563,6 +1576,7 @@ rb_zstream_flush_next_in(VALUE obj)
VALUE dst;

TypedData_Get_Struct(obj, struct zstream, &zstream_data_type, z);
zstream_check_not_in_progress(z);
dst = zstream_detach_input(z);
return dst;
}
Expand All @@ -1582,6 +1596,7 @@ rb_zstream_flush_next_out(VALUE obj)
struct zstream *z;

TypedData_Get_Struct(obj, struct zstream, &zstream_data_type, z);
zstream_check_not_in_progress(z);

return zstream_detach_buffer(z);
}
Expand Down Expand Up @@ -1612,6 +1627,7 @@ rb_zstream_set_avail_out(VALUE obj, VALUE size)

if (len < 0) rb_raise(rb_eArgError, "negative size");

zstream_check_not_in_progress(z);
zstream_expand_buffer_into(z, len);
return size;
}
Expand Down Expand Up @@ -1821,6 +1837,7 @@ rb_deflate_init_copy(VALUE self, VALUE orig)
z2 = get_zstream(orig);

if (z1 == z2) return self;
zstream_check_not_in_progress(z2);
err = deflateCopy(&z1->stream, &z2->stream);
if (err != Z_OK) {
raise_zlib_error(err, 0);
Expand Down Expand Up @@ -2032,6 +2049,7 @@ rb_deflate_params(VALUE obj, VALUE v_level, VALUE v_strategy)

level = ARG_LEVEL(v_level);
strategy = ARG_STRATEGY(v_strategy);
zstream_check_not_in_progress(z);

if (NIL_P(z->buf) || z->stream.avail_out == 0) {
zstream_expand_buffer(z);
Expand Down Expand Up @@ -2077,6 +2095,7 @@ rb_deflate_set_dictionary(VALUE obj, VALUE dic)
int err;

StringValue(src);
zstream_check_not_in_progress(z);
err = deflateSetDictionary(&z->stream,
(Bytef*)RSTRING_PTR(src), RSTRING_LENINT(src));
if (err != Z_OK) {
Expand Down Expand Up @@ -2443,6 +2462,7 @@ rb_inflate_sync(VALUE obj, VALUE src)
struct zstream *z = get_zstream(obj);

StringValue(src);
zstream_check_not_in_progress(z);
return zstream_sync(z, (Bytef*)RSTRING_PTR(src), RSTRING_LEN(src));
}

Expand All @@ -2459,6 +2479,7 @@ rb_inflate_sync_point_p(VALUE obj)
struct zstream *z = get_zstream(obj);
int err;

zstream_check_not_in_progress(z);
err = inflateSyncPoint(&z->stream);
if (err == 1) {
return Qtrue;
Expand All @@ -2481,6 +2502,17 @@ rb_inflate_set_dictionary(VALUE obj, VALUE dic)
{
struct zstream *z = get_zstream(obj);
VALUE src = dic;

StringValue(src);
zstream_check_not_in_progress(z);
inflate_set_dictionary(z, src);

return dic;
}

static void
inflate_set_dictionary(struct zstream *z, VALUE src)
{
int err;

StringValue(src);
Expand All @@ -2489,8 +2521,6 @@ rb_inflate_set_dictionary(VALUE obj, VALUE dic)
if (err != Z_OK) {
raise_zlib_error(err, z->stream.msg);
}

return dic;
}


Expand Down Expand Up @@ -3328,6 +3358,7 @@ get_gzfile(VALUE obj)
if (!ZSTREAM_IS_READY(&gz->z)) {
rb_raise(cGzError, "closed gzip stream");
}
zstream_check_not_in_progress(&gz->z);
return gz;
}

Expand Down Expand Up @@ -3675,6 +3706,7 @@ rb_gzfile_close(VALUE obj)
if (!ZSTREAM_IS_READY(&gz->z)) {
return Qnil;
}
zstream_check_not_in_progress(&gz->z);
io = gz->io;
gzfile_close(gz, 1);
return io;
Expand Down Expand Up @@ -4221,6 +4253,7 @@ rb_gzreader_unused(VALUE obj)
{
struct gzfile *gz;
TypedData_Get_Struct(obj, struct gzfile, &gzfile_data_type, gz);
zstream_check_not_in_progress(&gz->z);
return gzfile_reader_get_unused(gz);
}

Expand Down
55 changes: 55 additions & 0 deletions test/zlib/test_zlib.rb
Original file line number Diff line number Diff line change
Expand Up @@ -970,6 +970,40 @@ def test_in_progress_error_does_not_end_in_progress
zd.finish
end

def test_deflate_in_progress
zd = Zlib::Deflate.new
s = Random.new(0).bytes(100_000)
zd.deflate(s, Zlib::FINISH) do
assert_raise(Zlib::InProgressError) { zd.close }
assert_raise(Zlib::InProgressError) { zd.reset }
assert_raise(Zlib::InProgressError) { zd.flush_next_in }
assert_raise(Zlib::InProgressError) { zd.flush_next_out }
assert_raise(Zlib::InProgressError) { zd.avail_out = 10 }
assert_raise(Zlib::InProgressError) { zd.params(1, Zlib::DEFAULT_STRATEGY) }
assert_raise(Zlib::InProgressError) { zd.set_dictionary("foo") }
assert_raise(Zlib::InProgressError) { zd.dup }
break
end
zd.finish
end

def test_inflate_in_progress
zi = Zlib::Inflate.new
s = Zlib.deflate(Random.new(0).bytes(100_000))
zi.inflate(s) do
assert_raise(Zlib::InProgressError) { zi.close }
assert_raise(Zlib::InProgressError) { zi.reset }
assert_raise(Zlib::InProgressError) { zi.flush_next_in }
assert_raise(Zlib::InProgressError) { zi.flush_next_out }
assert_raise(Zlib::InProgressError) { zi.avail_out = 10 }
assert_raise(Zlib::InProgressError) { zi.sync("foo") }
assert_raise(Zlib::InProgressError) { zi.sync_point? }
assert_raise(Zlib::InProgressError) { zi.set_dictionary("foo") }
break
end
zi.close
end

def test_recursive_inflate
original_gc_stress = GC.stress
GC.stress = true
Expand Down Expand Up @@ -1932,6 +1966,27 @@ def test_thread_wakeup_interrupt
end

class TestZlibGzipWriter < Test::Unit::TestCase
def test_close_while_writing_in_another_thread
gz = Zlib::GzipWriter.new(StringIO.new(+""))
s = Random.new(0).bytes(1_000_000)
q = Thread::Queue.new
th = Thread.new do
gz.write(s)
q << true
10.times { gz.write(s) }
rescue Zlib::GzipFile::Error, Zlib::InProgressError
end
q.pop
begin
gz.close
rescue Zlib::InProgressError
Thread.pass
retry
end
th.join
assert_predicate(gz, :closed?)
end

def test_invalid_new
assert_raise(NoMethodError, "[ruby-dev:23228]") { Zlib::GzipWriter.new(nil).close }
assert_raise(NoMethodError, "[ruby-dev:23344]") { Zlib::GzipWriter.new(true).close }
Expand Down
Loading