Skip to content

Commit 37e9d84

Browse files
[3.13] Fix integer overflow for formats "s" and "p" in the struct module (GH-145750) (GH-145777)
(cherry picked from commit 4d0dce0)
1 parent f349433 commit 37e9d84

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

Lib/test/test_struct.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,12 @@ def test_count_overflow(self):
550550
hugecount3 = '{}i{}q'.format(sys.maxsize // 4, sys.maxsize // 8)
551551
self.assertRaises(struct.error, struct.calcsize, hugecount3)
552552

553+
hugecount4 = '{}?s'.format(sys.maxsize)
554+
self.assertRaises(struct.error, struct.calcsize, hugecount4)
555+
556+
hugecount5 = '{}?p'.format(sys.maxsize)
557+
self.assertRaises(struct.error, struct.calcsize, hugecount5)
558+
553559
def test_trailing_counter(self):
554560
store = array.array('b', b' '*100)
555561

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Avoid undefined behaviour from signed integer overflow when parsing format
2+
strings in the :mod:`struct` module. Found by OSS Fuzz in
3+
:oss-fuzz:`488466741`.

Modules/_struct.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1476,7 +1476,13 @@ prepare_s(PyStructObject *self, PyObject *format)
14761476

14771477
switch (c) {
14781478
case 's': /* fall through */
1479-
case 'p': len++; ncodes++; break;
1479+
case 'p':
1480+
if (len == PY_SSIZE_T_MAX) {
1481+
goto overflow;
1482+
}
1483+
len++;
1484+
ncodes++;
1485+
break;
14801486
case 'x': break;
14811487
default:
14821488
if (num > PY_SSIZE_T_MAX - len) {

0 commit comments

Comments
 (0)