https://github.com/python/cpython/commit/4d0dce0c8ddc4d0321bd590a1a33990edc2e1b08
commit: 4d0dce0c8ddc4d0321bd590a1a33990edc2e1b08
branch: main
author: Stan Ulbrych <[email protected]>
committer: serhiy-storchaka <[email protected]>
date: 2026-03-10T18:57:34+02:00
summary:
Fix integer overflow for formats "s" and "p" in the struct module (GH-145750)
files:
A Misc/NEWS.d/next/Library/2026-03-10-14-13-12.gh-issue-145750.iQsTeX.rst
M Lib/test/test_struct.py
M Modules/_struct.c
diff --git a/Lib/test/test_struct.py b/Lib/test/test_struct.py
index 6904572d095d31..55e2ce590a2577 100644
--- a/Lib/test/test_struct.py
+++ b/Lib/test/test_struct.py
@@ -555,6 +555,12 @@ def test_count_overflow(self):
hugecount3 = '{}i{}q'.format(sys.maxsize // 4, sys.maxsize // 8)
self.assertRaises(struct.error, struct.calcsize, hugecount3)
+ hugecount4 = '{}?s'.format(sys.maxsize)
+ self.assertRaises(struct.error, struct.calcsize, hugecount4)
+
+ hugecount5 = '{}?p'.format(sys.maxsize)
+ self.assertRaises(struct.error, struct.calcsize, hugecount5)
+
def test_trailing_counter(self):
store = array.array('b', b' '*100)
diff --git
a/Misc/NEWS.d/next/Library/2026-03-10-14-13-12.gh-issue-145750.iQsTeX.rst
b/Misc/NEWS.d/next/Library/2026-03-10-14-13-12.gh-issue-145750.iQsTeX.rst
new file mode 100644
index 00000000000000..a909bea2caffe9
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2026-03-10-14-13-12.gh-issue-145750.iQsTeX.rst
@@ -0,0 +1,3 @@
+Avoid undefined behaviour from signed integer overflow when parsing format
+strings in the :mod:`struct` module. Found by OSS Fuzz in
+:oss-fuzz:`488466741`.
diff --git a/Modules/_struct.c b/Modules/_struct.c
index c2f7b1fe0e800a..f8574322b40c8d 100644
--- a/Modules/_struct.c
+++ b/Modules/_struct.c
@@ -1676,7 +1676,13 @@ prepare_s(PyStructObject *self, PyObject *format)
switch (c) {
case 's': _Py_FALLTHROUGH;
- case 'p': len++; ncodes++; break;
+ case 'p':
+ if (len == PY_SSIZE_T_MAX) {
+ goto overflow;
+ }
+ len++;
+ ncodes++;
+ break;
case 'x': break;
default:
if (num > PY_SSIZE_T_MAX - len) {
_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]