[3.13] gh-121153: Fix some errors with use of _PyLong_CompactValue() (GH-121154) (GH-121900)

* The result has type Py_ssize_t, not intptr_t.
* Type cast between unsigned and signed integer types should be explicit.
* Downcasting should be explicit.
* Fix integer overflow check in sum().
(cherry picked from commit 1801545)
This commit is contained in:
Serhiy Storchaka 2024-07-17 11:04:45 +03:00 committed by GitHub
parent d358f74a69
commit 09ff4ec14f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 57 additions and 15 deletions

View file

@ -2601,8 +2601,8 @@ builtin_sum_impl(PyObject *module, PyObject *iterable, PyObject *start)
b = PyLong_AsLongAndOverflow(item, &overflow);
}
if (overflow == 0 &&
(i_result >= 0 ? (b <= LONG_MAX - i_result)
: (b >= LONG_MIN - i_result)))
(i_result >= 0 ? (b <= PY_SSIZE_T_MAX - i_result)
: (b >= PY_SSIZE_T_MIN - i_result)))
{
i_result += b;
Py_DECREF(item);