Skip to content

Commit 42cd056

Browse files
StanFromIrelandpicnixz
authored andcommitted
gh-156180: Reject a negative len2 in zlib.{adler32,crc32}_combine() (GH-156181)
(cherry picked from commit b062727097e997bcb900e11503d3248daac903da) Co-authored-by: Stan Ulbrych <stan@python.org> Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
1 parent e839679 commit 42cd056

4 files changed

Lines changed: 20 additions & 2 deletions

File tree

.github/CODEOWNERS

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,12 @@ Lib/test/test_unittest/testmock/ @cjw296
624624
# Weakref
625625
**/*weakref* @kumaraditya303
626626

627+
# Zlib
628+
Doc/library/zlib.rst @StanFromIreland
629+
Lib/compression/zlib.py @StanFromIreland
630+
Lib/test/test_zlib.py @StanFromIreland
631+
Modules/_zlibmodule.c @StanFromIreland
632+
627633
# Zipfile.Path
628634
Lib/test/test_zipfile/_path/ @jaraco
629635
Lib/zipfile/_path/ @jaraco

Lib/test/test_zlib.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,9 @@ 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(OverflowError, self.combine, 0, 0, 2**1000)
187+
self.assertRaises(OverflowError, self.combine, 0, 0, -2**1000)
185188

186189
def test_combine_with_iv(self):
187190
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.adler32_combine` and :func:`zlib.crc32_combine` now raise
2+
:exc:`ValueError` if the *len2* argument is negative, instead of returning
3+
rubbish or hanging indefinitely, respectively.

Modules/zlibmodule.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1948,7 +1948,10 @@ zlib_adler32_combine_impl(PyObject *module, unsigned int adler1,
19481948
#else
19491949
z_off_t len = convert_to_z_off_t(len2);
19501950
#endif
1951-
if (PyErr_Occurred()) {
1951+
if (len < 0) {
1952+
if (!PyErr_Occurred()) {
1953+
PyErr_SetString(PyExc_ValueError, "len2 must be non-negative");
1954+
}
19521955
return (unsigned int)-1;
19531956
}
19541957
return adler32_combine(adler1, adler2, len);
@@ -2033,7 +2036,10 @@ zlib_crc32_combine_impl(PyObject *module, unsigned int crc1,
20332036
#else
20342037
z_off_t len = convert_to_z_off_t(len2);
20352038
#endif
2036-
if (PyErr_Occurred()) {
2039+
if (len < 0) {
2040+
if (!PyErr_Occurred()) {
2041+
PyErr_SetString(PyExc_ValueError, "len2 must be non-negative");
2042+
}
20372043
return (unsigned int)-1;
20382044
}
20392045
return crc32_combine(crc1, crc2, len);

0 commit comments

Comments
 (0)