Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e8db861

Browse files
committed
Issue #27581: Don’t rely on overflow wrapping in PySequence_Tuple()
Patch by Xiang Zhang.
1 parent aa46bd4 commit e8db861

2 files changed

Lines changed: 8 additions & 4 deletions

File tree

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ Core and Builtins
2525
- Issue #27507: Add integer overflow check in bytearray.extend(). Patch by
2626
Xiang Zhang.
2727

28+
- Issue #27581: Don't rely on wrapping for overflow check in
29+
PySequence_Tuple(). Patch by Xiang Zhang.
30+
2831
- Issue #27443: __length_hint__() of bytearray iterators no longer return a
2932
negative integer for a resized bytearray.
3033

Objects/abstract.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1724,21 +1724,22 @@ PySequence_Tuple(PyObject *v)
17241724
break;
17251725
}
17261726
if (j >= n) {
1727-
Py_ssize_t oldn = n;
1727+
size_t newn = (size_t)n;
17281728
/* The over-allocation strategy can grow a bit faster
17291729
than for lists because unlike lists the
17301730
over-allocation isn't permanent -- we reclaim
17311731
the excess before the end of this routine.
17321732
So, grow by ten and then add 25%.
17331733
*/
1734-
n += 10;
1735-
n += n >> 2;
1736-
if (n < oldn) {
1734+
newn += 10u;
1735+
newn += newn >> 2;
1736+
if (newn > PY_SSIZE_T_MAX) {
17371737
/* Check for overflow */
17381738
PyErr_NoMemory();
17391739
Py_DECREF(item);
17401740
goto Fail;
17411741
}
1742+
n = (Py_ssize_t)newn;
17421743
if (_PyTuple_Resize(&result, n) != 0) {
17431744
Py_DECREF(item);
17441745
goto Fail;

0 commit comments

Comments
 (0)