diff --git a/ext/zlib/zlib.c b/ext/zlib/zlib.c index c0190dc..259929e 100644 --- a/ext/zlib/zlib.c +++ b/ext/zlib/zlib.c @@ -546,8 +546,15 @@ rb_zlib_crc32(int argc, VALUE *argv, VALUE klass) static VALUE rb_zlib_crc32_combine(VALUE klass, VALUE crc1, VALUE crc2, VALUE len2) { + z_off_t len = (z_off_t)NUM2ULEN(len2); + + /* zlib 1.3.1 loops forever on a negative length. NUM2ULEN wraps a + * negative len2, which stays positive when z_off_t is the wider type. */ + if (len < 0 || RTEST(rb_funcall(len2, '<', 1, INT2FIX(0)))) { + rb_raise(rb_eArgError, "negative length"); + } return ULEN2NUM( - crc32_combine(NUM2ULONG(crc1), NUM2ULONG(crc2), NUM2ULEN(len2))); + crc32_combine(NUM2ULONG(crc1), NUM2ULONG(crc2), len)); } #else #define rb_zlib_crc32_combine rb_f_notimplement diff --git a/test/zlib/test_zlib.rb b/test/zlib/test_zlib.rb index edb35f9..027054b 100644 --- a/test/zlib/test_zlib.rb +++ b/test/zlib/test_zlib.rb @@ -2107,6 +2107,12 @@ def test_crc32_combine end end + def test_crc32_combine_negative_length + assert_raise(ArgumentError) { Zlib.crc32_combine(0, 0, -1) } + rescue NotImplementedError + omit "crc32_combine is not implemented" + end + def test_crc_table t = Zlib.crc_table assert_instance_of(Array, t)