Skip to content

Commit 9ba5e69

Browse files
Reject a negative len2 in zlib.{adler32,crc32}_combine()
1 parent 67f4d53 commit 9ba5e69

4 files changed

Lines changed: 19 additions & 0 deletions

File tree

.github/CODEOWNERS

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,12 @@ Lib/test/test_unittest/testmock/ @cjw296
620620
# Weakref
621621
**/*weakref* @kumaraditya303
622622

623+
# Zlib
624+
Doc/library/zlib.rst @StanFromIreland
625+
Lib/compression/zlib.py @StanFromIreland
626+
Lib/test/test_zlib.py @StanFromIreland
627+
Modules/_zlibmodule.c @StanFromIreland
628+
623629
# Zipfile.Path
624630
Lib/test/test_zipfile/_path/ @jaraco
625631
Lib/zipfile/_path/ @jaraco

Lib/test/test_zlib.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,8 @@ def test_combine_no_iv_invalid_length(self):
182182
self.assertNotEqual(invalid_res, checksum)
183183

184184
self.assertRaises(TypeError, self.combine, 0, 0, "len")
185+
self.assertRaises(ValueError, self.combine, 0, 0, -1)
186+
self.assertRaises(ValueError, self.combine, 0, 0, -2**32)
185187

186188
def test_combine_with_iv(self):
187189
for _ in range(self.N):
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
:func:`zlib.crc32_combine` and :func:`zlib.adler32_combine` now raise
2+
:exc:`ValueError` if the *len2* argument is negative, instead of hanging
3+
indefinitely or returning rubbish, respectively.

Modules/zlibmodule.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1951,6 +1951,10 @@ zlib_adler32_combine_impl(PyObject *module, unsigned int adler1,
19511951
if (PyErr_Occurred()) {
19521952
return (unsigned int)-1;
19531953
}
1954+
if (len < 0) {
1955+
PyErr_SetString(PyExc_ValueError, "len2 must be non-negative");
1956+
return (unsigned int)-1;
1957+
}
19541958
return adler32_combine(adler1, adler2, len);
19551959
}
19561960

@@ -2036,6 +2040,10 @@ zlib_crc32_combine_impl(PyObject *module, unsigned int crc1,
20362040
if (PyErr_Occurred()) {
20372041
return (unsigned int)-1;
20382042
}
2043+
if (len < 0) {
2044+
PyErr_SetString(PyExc_ValueError, "len2 must be non-negative");
2045+
return (unsigned int)-1;
2046+
}
20392047
return crc32_combine(crc1, crc2, len);
20402048
}
20412049

0 commit comments

Comments
 (0)