Skip to content
Open
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
12 changes: 12 additions & 0 deletions ext/zlib/zlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -1368,6 +1368,14 @@ get_zstream(VALUE obj)
return z;
}

static void
zstream_check_reinitialize(VALUE obj, struct zstream *z)
{
if (ZSTREAM_IS_READY(z)) {
rb_raise(rb_eRuntimeError, "reinitializing %"PRIsVALUE, rb_obj_class(obj));
}
}


/* ------------------------------------------------------------------------- */

Expand Down Expand Up @@ -1714,6 +1722,7 @@ rb_deflate_initialize(int argc, VALUE *argv, VALUE obj)

rb_scan_args(argc, argv, "04", &level, &wbits, &memlevel, &strategy);
TypedData_Get_Struct(obj, struct zstream, &zstream_data_type, z);
zstream_check_reinitialize(obj, z);

err = deflateInit2(&z->stream, ARG_LEVEL(level), Z_DEFLATED,
ARG_WBITS(wbits), ARG_MEMLEVEL(memlevel),
Expand Down Expand Up @@ -2070,6 +2079,7 @@ rb_inflate_initialize(int argc, VALUE *argv, VALUE obj)

rb_scan_args(argc, argv, "01", &wbits);
TypedData_Get_Struct(obj, struct zstream, &zstream_data_type, z);
zstream_check_reinitialize(obj, z);

err = inflateInit2(&z->stream, ARG_WBITS(wbits));
if (err != Z_OK) {
Expand Down Expand Up @@ -3805,6 +3815,7 @@ rb_gzwriter_initialize(int argc, VALUE *argv, VALUE obj)

rb_scan_args(argc, argv, "12", &io, &level, &strategy);
TypedData_Get_Struct(obj, struct gzfile, &gzfile_data_type, gz);
zstream_check_reinitialize(obj, &gz->z);

/* this is undocumented feature of zlib */
gz->level = ARG_LEVEL(level);
Expand Down Expand Up @@ -4067,6 +4078,7 @@ rb_gzreader_initialize(int argc, VALUE *argv, VALUE obj)
int err;

TypedData_Get_Struct(obj, struct gzfile, &gzfile_data_type, gz);
zstream_check_reinitialize(obj, &gz->z);
rb_scan_args(argc, argv, "1:", &io, &opt);

/* this is undocumented feature of zlib */
Expand Down
15 changes: 15 additions & 0 deletions test/zlib/test_zlib.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1558,6 +1558,21 @@ def ary.write(*args)
end

class TestZlib < Test::Unit::TestCase
def test_reinitialize
z = Zlib::Deflate.new
assert_raise(RuntimeError) { z.send(:initialize) }
z.close
z = Zlib::Inflate.new
assert_raise(RuntimeError) { z.send(:initialize) }
z.close
gz = Zlib::GzipWriter.new(StringIO.new(+""))
assert_raise(RuntimeError) { gz.send(:initialize, StringIO.new(+"")) }
gz.close
gz = Zlib::GzipReader.new(StringIO.new(Zlib.gzip("foo")))
assert_raise(RuntimeError) { gz.send(:initialize, StringIO.new(Zlib.gzip("foo"))) }
gz.close
end

def test_version
assert_instance_of(String, Zlib.zlib_version)
end
Expand Down
Loading