mirror of
https://github.com/python/cpython.git
synced 2026-04-14 07:41:00 +00:00
[3.14] _struct.c: Fix UB from integer overflow in prepare_s (GH-145158) (#145162)
`_struct.c`: Fix UB from integer overflow in `prepare_s` (GH-145158)
Avoid possible undefined behaviour from signed overflow in `struct` module
As discovered via oss-fuzz.
(cherry picked from commit fd0400585e)
Co-authored-by: Stan Ulbrych <89152624+StanFromIreland@users.noreply.github.com>
This commit is contained in:
parent
1f3ea54368
commit
35a7a6767e
3 changed files with 14 additions and 1 deletions
|
|
@ -552,6 +552,9 @@ def test_count_overflow(self):
|
|||
hugecount2 = '{}b{}H'.format(sys.maxsize//2, sys.maxsize//2)
|
||||
self.assertRaises(struct.error, struct.calcsize, hugecount2)
|
||||
|
||||
hugecount3 = '{}i{}q'.format(sys.maxsize // 4, sys.maxsize // 8)
|
||||
self.assertRaises(struct.error, struct.calcsize, hugecount3)
|
||||
|
||||
def test_trailing_counter(self):
|
||||
store = array.array('b', b' '*100)
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,2 @@
|
|||
Avoid undefined behaviour from signed integer overflow when parsing format
|
||||
strings in the :mod:`struct` module.
|
||||
|
|
@ -1678,7 +1678,15 @@ prepare_s(PyStructObject *self)
|
|||
case 's': _Py_FALLTHROUGH;
|
||||
case 'p': len++; ncodes++; break;
|
||||
case 'x': break;
|
||||
default: len += num; if (num) ncodes++; break;
|
||||
default:
|
||||
if (num > PY_SSIZE_T_MAX - len) {
|
||||
goto overflow;
|
||||
}
|
||||
len += num;
|
||||
if (num) {
|
||||
ncodes++;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
itemsize = e->size;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue