From 1cd073ffa3b0f82400394af2ab9df2a83362f688 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Thu, 12 Nov 2026 19:40:38 +0100 Subject: [PATCH 1/7] gh-161694: Check for integer overflow in _PyObject_VAR_SIZE() Check also for integer overflow in _PyType_AllocNoTrack(). Remove redundant (and incomplete) integer overflow from tuple_alloc() and _PyTuple_Resize(). --- Include/cpython/objimpl.h | 27 ++++++++++++++++--- Include/objimpl.h | 2 -- ...-11-12-21-34-55.gh-issue-161694.71tZlp.rst | 3 +++ Objects/tupleobject.c | 18 +++---------- Objects/typeobject.c | 15 ++++++++--- Python/gc.c | 2 +- Python/gc_free_threading.c | 2 +- 7 files changed, 43 insertions(+), 26 deletions(-) create mode 100644 Misc/NEWS.d/next/C_API/2026-11-12-21-34-55.gh-issue-161694.71tZlp.rst diff --git a/Include/cpython/objimpl.h b/Include/cpython/objimpl.h index e0c2ce286f13ce3..0b09326f09a7959 100644 --- a/Include/cpython/objimpl.h +++ b/Include/cpython/objimpl.h @@ -15,15 +15,34 @@ static inline size_t _PyObject_SIZE(PyTypeObject *type) { Note that there's no memory wastage in doing this, as malloc has to return (at worst) pointer-aligned memory anyway. + + Return SIZE_MAX on integer overflow (if the result would be greater than + PY_SSIZE_T_MAX). */ #if ((SIZEOF_VOID_P - 1) & SIZEOF_VOID_P) != 0 # error "_PyObject_VAR_SIZE requires SIZEOF_VOID_P be a power of 2" #endif -static inline size_t _PyObject_VAR_SIZE(PyTypeObject *type, Py_ssize_t nitems) { - size_t size = _Py_STATIC_CAST(size_t, type->tp_basicsize); - size += _Py_STATIC_CAST(size_t, nitems) * _Py_STATIC_CAST(size_t, type->tp_itemsize); - return _Py_SIZE_ROUND_UP(size, SIZEOF_VOID_P); +static inline size_t +_PyObject_VAR_SIZE(PyTypeObject *type, Py_ssize_t nitems_signed) +{ + assert(type->tp_itemsize >= 0); + + size_t basicsize = _Py_STATIC_CAST(size_t, type->tp_basicsize); + size_t nitems = _Py_STATIC_CAST(size_t, nitems_signed); + size_t itemsize = _Py_STATIC_CAST(size_t, type->tp_itemsize); + if (itemsize != 0) { + if (nitems > ((size_t)PY_SSIZE_T_MAX - basicsize) / itemsize) { + return SIZE_MAX; + } + } + size_t size = basicsize + nitems * itemsize; + + size = _Py_SIZE_ROUND_UP(size, SIZEOF_VOID_P); + if (size > (size_t)PY_SSIZE_T_MAX) { + return SIZE_MAX; + } + return size; } diff --git a/Include/objimpl.h b/Include/objimpl.h index 56472a72e42d341..54d5ba500ddb498 100644 --- a/Include/objimpl.h +++ b/Include/objimpl.h @@ -160,8 +160,6 @@ PyAPI_FUNC(PyVarObject *) _PyObject_GC_Resize(PyVarObject *, Py_ssize_t); #define PyObject_GC_Resize(type, op, n) \ ( (type *) _PyObject_GC_Resize(_PyVarObject_CAST(op), (n)) ) - - PyAPI_FUNC(PyObject *) _PyObject_GC_New(PyTypeObject *); PyAPI_FUNC(PyVarObject *) _PyObject_GC_NewVar(PyTypeObject *, Py_ssize_t); diff --git a/Misc/NEWS.d/next/C_API/2026-11-12-21-34-55.gh-issue-161694.71tZlp.rst b/Misc/NEWS.d/next/C_API/2026-11-12-21-34-55.gh-issue-161694.71tZlp.rst new file mode 100644 index 000000000000000..06efe3f6c25b72b --- /dev/null +++ b/Misc/NEWS.d/next/C_API/2026-11-12-21-34-55.gh-issue-161694.71tZlp.rst @@ -0,0 +1,3 @@ +c:func:`_PyObject_VAR_SIZE` now returns ``SIZE_MAX`` on integer overflow (if +the result would be greater than ``PY_SSIZE_T_MAX``). Patch by Victor +Stinner. diff --git a/Objects/tupleobject.c b/Objects/tupleobject.c index 992699756ac770f..4b096d209fd92eb 100644 --- a/Objects/tupleobject.c +++ b/Objects/tupleobject.c @@ -50,11 +50,6 @@ tuple_alloc(Py_ssize_t size) return op; } } - /* Check for overflow */ - if ((size_t)size > ((size_t)PY_SSIZE_T_MAX - (sizeof(PyTupleObject) - - sizeof(PyObject *))) / sizeof(PyObject *)) { - return (PyTupleObject *)PyErr_NoMemory(); - } PyTupleObject *result = PyObject_GC_NewVar(PyTupleObject, &PyTuple_Type, size); if (result != NULL) { _PyTuple_RESET_HASH_CACHE(result); @@ -1079,14 +1074,6 @@ _PyTuple_Resize(PyObject **pv, Py_ssize_t newsize) *pv = PyTuple_New(newsize); return *pv == NULL ? -1 : 0; } - /* Check for overflow */ - if ((size_t)newsize > ((size_t)PY_SSIZE_T_MAX - (sizeof(PyTupleObject) - - sizeof(PyObject *))) / sizeof(PyObject *)) { - *pv = NULL; - Py_DECREF(v); - PyErr_NoMemory(); - return -1; - } if (_PyObject_GC_IS_TRACKED(v)) { _PyObject_GC_UNTRACK(v); @@ -1315,12 +1302,13 @@ maybe_freelist_push(PyTupleObject *op) void _PyTuple_DebugMallocStats(FILE *out) { + PyTypeObject *type = &PyTuple_Type; for (int i = 0; i < PyTuple_MAXSAVESIZE; i++) { int len = i + 1; char buf[128]; PyOS_snprintf(buf, sizeof(buf), "free %d-sized PyTupleObject", len); - _PyDebugAllocatorStats(out, buf, _Py_FREELIST_SIZE(tuples[i]), - _PyType_PreHeaderSize(&PyTuple_Type) + _PyObject_VAR_SIZE(&PyTuple_Type, len)); + size_t size = _PyType_PreHeaderSize(type) + _PyObject_VAR_SIZE(type, len); + _PyDebugAllocatorStats(out, buf, _Py_FREELIST_SIZE(tuples[i]), size); } } diff --git a/Objects/typeobject.c b/Objects/typeobject.c index ee8c2090e6397d7..b84ed846cb433f2 100644 --- a/Objects/typeobject.c +++ b/Objects/typeobject.c @@ -2546,13 +2546,22 @@ _PyType_AllocNoTrack(PyTypeObject *type, Py_ssize_t nitems) * tuple. See GH-100659 and GH-81381. */ size_t size = _PyObject_VAR_SIZE(type, nitems+1); - const size_t presize = _PyType_PreHeaderSize(type); if (type->tp_flags & Py_TPFLAGS_INLINE_VALUES) { assert(type->tp_itemsize == 0); - size += _PyInlineValuesSize(type); + size_t inline_values_size = _PyInlineValuesSize(type); + if (size > (size_t)PY_SSIZE_T_MAX - inline_values_size) { + return PyErr_NoMemory(); + } + size += inline_values_size; } + + const size_t presize = _PyType_PreHeaderSize(type); + if (size > (size_t)PY_SSIZE_T_MAX - presize) { + return PyErr_NoMemory(); + } + char *alloc = _PyObject_MallocWithType(type, size + presize); - if (alloc == NULL) { + if (alloc == NULL) { return PyErr_NoMemory(); } obj = (PyObject *)(alloc + presize); diff --git a/Python/gc.c b/Python/gc.c index e363fa77f724f88..5b9028c8cab541e 100644 --- a/Python/gc.c +++ b/Python/gc.c @@ -2067,7 +2067,7 @@ static PyObject * gc_alloc(PyTypeObject *tp, size_t basicsize, size_t presize) { PyThreadState *tstate = _PyThreadState_GET(); - if (basicsize > PY_SSIZE_T_MAX - presize) { + if (basicsize > (size_t)PY_SSIZE_T_MAX - presize) { return _PyErr_NoMemory(tstate); } size_t size = presize + basicsize; diff --git a/Python/gc_free_threading.c b/Python/gc_free_threading.c index f4f1a7777f35223..9c4d128af4f53d8 100644 --- a/Python/gc_free_threading.c +++ b/Python/gc_free_threading.c @@ -2775,7 +2775,7 @@ static PyObject * gc_alloc(PyTypeObject *tp, size_t basicsize, size_t presize) { PyThreadState *tstate = _PyThreadState_GET(); - if (basicsize > PY_SSIZE_T_MAX - presize) { + if (basicsize > (size_t)PY_SSIZE_T_MAX - presize) { return _PyErr_NoMemory(tstate); } size_t size = presize + basicsize; From bad2045f60f0391370504297dee4ed62192ebdce Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Thu, 12 Nov 2026 21:59:11 +0100 Subject: [PATCH 2/7] Fix NEWS entry --- .../C_API/2026-11-12-21-34-55.gh-issue-161694.71tZlp.rst | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Misc/NEWS.d/next/C_API/2026-11-12-21-34-55.gh-issue-161694.71tZlp.rst b/Misc/NEWS.d/next/C_API/2026-11-12-21-34-55.gh-issue-161694.71tZlp.rst index 06efe3f6c25b72b..73ac019d948a4b4 100644 --- a/Misc/NEWS.d/next/C_API/2026-11-12-21-34-55.gh-issue-161694.71tZlp.rst +++ b/Misc/NEWS.d/next/C_API/2026-11-12-21-34-55.gh-issue-161694.71tZlp.rst @@ -1,3 +1,2 @@ -c:func:`_PyObject_VAR_SIZE` now returns ``SIZE_MAX`` on integer overflow (if -the result would be greater than ``PY_SSIZE_T_MAX``). Patch by Victor -Stinner. +c:func:`!_PyObject_VAR_SIZE` now returns ``SIZE_MAX`` on integer overflow (if +the result would be greater than ``PY_SSIZE_T_MAX``). Patch by Victor Stinner. From 5998a8d25abc4d023a420cc74376f6618c414ad5 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Thu, 12 Nov 2026 21:59:16 +0100 Subject: [PATCH 3/7] Check also overflow on nitems+1 in _PyType_AllocNoTrack() In debug mode, check also that nitems >= 0 in _PyType_AllocNoTrack() and _PyObject_GC_Resize(). --- Objects/typeobject.c | 13 +++++++++++-- Python/gc.c | 7 +++++++ Python/gc_free_threading.c | 7 +++++++ 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/Objects/typeobject.c b/Objects/typeobject.c index b84ed846cb433f2..68908c3f0bae626 100644 --- a/Objects/typeobject.c +++ b/Objects/typeobject.c @@ -2537,13 +2537,22 @@ _PyType_NewManagedObject(PyTypeObject *type) PyObject * _PyType_AllocNoTrack(PyTypeObject *type, Py_ssize_t nitems) { - PyObject *obj; +#ifdef Py_DEBUG + if (nitems < 0) { + PyErr_BadInternalCall(); + return NULL; + } +#endif + /* The +1 on nitems is needed for most types but not all. We could save a * bit of space by allocating one less item in certain cases, depending on * the type. However, given the extra complexity (e.g. an additional type * flag to indicate when that is safe) it does not seem worth the memory * savings. An example type that doesn't need the +1 is a subclass of * tuple. See GH-100659 and GH-81381. */ + if (nitems > PY_SSIZE_T_MAX - 1) { + return PyErr_NoMemory(); + } size_t size = _PyObject_VAR_SIZE(type, nitems+1); if (type->tp_flags & Py_TPFLAGS_INLINE_VALUES) { @@ -2564,7 +2573,7 @@ _PyType_AllocNoTrack(PyTypeObject *type, Py_ssize_t nitems) if (alloc == NULL) { return PyErr_NoMemory(); } - obj = (PyObject *)(alloc + presize); + PyObject *obj = (PyObject *)(alloc + presize); if (presize) { ((PyObject **)alloc)[0] = NULL; ((PyObject **)alloc)[1] = NULL; diff --git a/Python/gc.c b/Python/gc.c index 5b9028c8cab541e..aa727fc7a77d6a3 100644 --- a/Python/gc.c +++ b/Python/gc.c @@ -2138,6 +2138,13 @@ PyUnstable_Object_GC_NewWithExtraData(PyTypeObject *tp, size_t extra_size) PyVarObject * _PyObject_GC_Resize(PyVarObject *op, Py_ssize_t nitems) { +#ifdef Py_DEBUG + if (nitems < 0) { + PyErr_BadInternalCall(); + return NULL; + } +#endif + const size_t basicsize = _PyObject_VAR_SIZE(Py_TYPE(op), nitems); const size_t presize = _PyType_PreHeaderSize(Py_TYPE(op)); _PyObject_ASSERT((PyObject *)op, !_PyObject_GC_IS_TRACKED(op)); diff --git a/Python/gc_free_threading.c b/Python/gc_free_threading.c index 9c4d128af4f53d8..0d27224e59b3da8 100644 --- a/Python/gc_free_threading.c +++ b/Python/gc_free_threading.c @@ -2847,6 +2847,13 @@ PyUnstable_Object_GC_NewWithExtraData(PyTypeObject *tp, size_t extra_size) PyVarObject * _PyObject_GC_Resize(PyVarObject *op, Py_ssize_t nitems) { +#ifdef Py_DEBUG + if (nitems < 0) { + PyErr_BadInternalCall(); + return NULL; + } +#endif + const size_t basicsize = _PyObject_VAR_SIZE(Py_TYPE(op), nitems); const size_t presize = _PyType_PreHeaderSize(((PyObject *)op)->ob_type); _PyObject_ASSERT((PyObject *)op, !_PyObject_GC_IS_TRACKED(op)); From 3b6abf719ddd840c2a145c2d498c07660b70842f Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Thu, 12 Nov 2026 22:01:10 +0100 Subject: [PATCH 4/7] Fix test_cext Fix compiler warning: Include/cpython/objimpl.h:31:5: error: ISO C90 forbids mixed declarations and code [-Werror=declaration-after-statement] --- Include/cpython/objimpl.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Include/cpython/objimpl.h b/Include/cpython/objimpl.h index 0b09326f09a7959..f0f5414ba2dae03 100644 --- a/Include/cpython/objimpl.h +++ b/Include/cpython/objimpl.h @@ -31,12 +31,13 @@ _PyObject_VAR_SIZE(PyTypeObject *type, Py_ssize_t nitems_signed) size_t basicsize = _Py_STATIC_CAST(size_t, type->tp_basicsize); size_t nitems = _Py_STATIC_CAST(size_t, nitems_signed); size_t itemsize = _Py_STATIC_CAST(size_t, type->tp_itemsize); + size_t size; if (itemsize != 0) { if (nitems > ((size_t)PY_SSIZE_T_MAX - basicsize) / itemsize) { return SIZE_MAX; } } - size_t size = basicsize + nitems * itemsize; + size = basicsize + nitems * itemsize; size = _Py_SIZE_ROUND_UP(size, SIZEOF_VOID_P); if (size > (size_t)PY_SSIZE_T_MAX) { From e9f628015ee360066bd6b72a6de9cf571f4b11e4 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Thu, 12 Nov 2026 22:04:14 +0100 Subject: [PATCH 5/7] Micro-optimize Don't compute nitems * itemsize if tp_itemsize is zero. --- Include/cpython/objimpl.h | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/Include/cpython/objimpl.h b/Include/cpython/objimpl.h index f0f5414ba2dae03..d3df469a8184a6e 100644 --- a/Include/cpython/objimpl.h +++ b/Include/cpython/objimpl.h @@ -29,15 +29,18 @@ _PyObject_VAR_SIZE(PyTypeObject *type, Py_ssize_t nitems_signed) assert(type->tp_itemsize >= 0); size_t basicsize = _Py_STATIC_CAST(size_t, type->tp_basicsize); - size_t nitems = _Py_STATIC_CAST(size_t, nitems_signed); - size_t itemsize = _Py_STATIC_CAST(size_t, type->tp_itemsize); size_t size; - if (itemsize != 0) { + if (type->tp_itemsize != 0) { + size_t nitems = _Py_STATIC_CAST(size_t, nitems_signed); + size_t itemsize = _Py_STATIC_CAST(size_t, type->tp_itemsize); if (nitems > ((size_t)PY_SSIZE_T_MAX - basicsize) / itemsize) { return SIZE_MAX; } + size = basicsize + nitems * itemsize; + } + else { + size = basicsize; } - size = basicsize + nitems * itemsize; size = _Py_SIZE_ROUND_UP(size, SIZEOF_VOID_P); if (size > (size_t)PY_SSIZE_T_MAX) { From e1e0a17dcbce1712d643c1603a3643f380373ce0 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Fri, 13 Nov 2026 04:36:26 +0100 Subject: [PATCH 6/7] Fix typo in the Changelog entry --- .../next/C_API/2026-11-12-21-34-55.gh-issue-161694.71tZlp.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/C_API/2026-11-12-21-34-55.gh-issue-161694.71tZlp.rst b/Misc/NEWS.d/next/C_API/2026-11-12-21-34-55.gh-issue-161694.71tZlp.rst index 73ac019d948a4b4..e82a0a267cfe499 100644 --- a/Misc/NEWS.d/next/C_API/2026-11-12-21-34-55.gh-issue-161694.71tZlp.rst +++ b/Misc/NEWS.d/next/C_API/2026-11-12-21-34-55.gh-issue-161694.71tZlp.rst @@ -1,2 +1,2 @@ -c:func:`!_PyObject_VAR_SIZE` now returns ``SIZE_MAX`` on integer overflow (if +:c:func:`!_PyObject_VAR_SIZE` now returns ``SIZE_MAX`` on integer overflow (if the result would be greater than ``PY_SSIZE_T_MAX``). Patch by Victor Stinner. From 58e96571be3424570d9ce9d6d68f65b5ad42528d Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Fri, 13 Nov 2026 04:38:04 +0100 Subject: [PATCH 7/7] Fix test_cext for "ISO C90 forbids mixed declarations and code" --- Include/cpython/objimpl.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Include/cpython/objimpl.h b/Include/cpython/objimpl.h index d3df469a8184a6e..49f73ad73eae01b 100644 --- a/Include/cpython/objimpl.h +++ b/Include/cpython/objimpl.h @@ -26,10 +26,12 @@ static inline size_t _PyObject_SIZE(PyTypeObject *type) { static inline size_t _PyObject_VAR_SIZE(PyTypeObject *type, Py_ssize_t nitems_signed) { + size_t basicsize; + size_t size; + assert(type->tp_itemsize >= 0); + basicsize = _Py_STATIC_CAST(size_t, type->tp_basicsize); - size_t basicsize = _Py_STATIC_CAST(size_t, type->tp_basicsize); - size_t size; if (type->tp_itemsize != 0) { size_t nitems = _Py_STATIC_CAST(size_t, nitems_signed); size_t itemsize = _Py_STATIC_CAST(size_t, type->tp_itemsize);