Skip to content

Commit d1f7692

Browse files
authored
Merge pull request #160 from mame/zlib-crc32-combine-negative
Reject a negative length in Zlib.crc32_combine
2 parents 40e85f2 + 5943150 commit d1f7692

2 files changed

Lines changed: 14 additions & 1 deletion

File tree

‎ext/zlib/zlib.c‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -546,8 +546,15 @@ rb_zlib_crc32(int argc, VALUE *argv, VALUE klass)
546546
static VALUE
547547
rb_zlib_crc32_combine(VALUE klass, VALUE crc1, VALUE crc2, VALUE len2)
548548
{
549+
z_off_t len = (z_off_t)NUM2ULEN(len2);
550+
551+
/* zlib 1.3.1 loops forever on a negative length. NUM2ULEN wraps a
552+
* negative len2, which stays positive when z_off_t is the wider type. */
553+
if (len < 0 || RTEST(rb_funcall(len2, '<', 1, INT2FIX(0)))) {
554+
rb_raise(rb_eArgError, "negative length");
555+
}
549556
return ULEN2NUM(
550-
crc32_combine(NUM2ULONG(crc1), NUM2ULONG(crc2), NUM2ULEN(len2)));
557+
crc32_combine(NUM2ULONG(crc1), NUM2ULONG(crc2), len));
551558
}
552559
#else
553560
#define rb_zlib_crc32_combine rb_f_notimplement

‎test/zlib/test_zlib.rb‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2110,6 +2110,12 @@ def test_crc32_combine
21102110
end
21112111
end
21122112

2113+
def test_crc32_combine_negative_length
2114+
assert_raise(ArgumentError) { Zlib.crc32_combine(0, 0, -1) }
2115+
rescue NotImplementedError
2116+
omit "crc32_combine is not implemented"
2117+
end
2118+
21132119
def test_crc_table
21142120
t = Zlib.crc_table
21152121
assert_instance_of(Array, t)

0 commit comments

Comments
 (0)