10000 bpo-34397: Remove redundant overflow checks in list and tuple impleme… · python/cpython@e682b26 · GitHub
[go: up one dir, main page]

Skip to content

Commit e682b26

Browse files
authored
bpo-34397: Remove redundant overflow checks in list and tuple implementation. (GH-8757)
1 parent ef16958 commit e682b26

File tree

2 files changed

+4
-15
lines changed

2 files changed

+4
-15
lines changed

Objects/listobject.c

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -261,12 +261,8 @@ ins1(PyListObject *self, Py_ssize_t where, PyObject *v)
261261
PyErr_BadInternalCall();
262262
return -1;
263263
}
264-
if (n == PY_SSIZE_T_MAX) {
265-
PyErr_SetString(PyExc_OverflowError,
266-
"cannot add more objects to list");
267-
return -1;
268-
}
269264

265+
assert((size_t)n + 1 < PY_SSIZE_T_MAX);
270266
if (list_resize(self, n+1) < 0)
271267
return -1;
272268

@@ -301,12 +297,7 @@ app1(PyListObject *self, PyObject *v)
301297
Py_ssize_t n = PyList_GET_SIZE(self);
302298

303299
assert (v != NULL);
304-
if (n == PY_SSIZE_T_MAX) {
305-
PyErr_SetString(PyExc_OverflowError,
306-
"cannot add more objects to list");
307-
return -1;
308-
}
309-
300+
assert((size_t)n + 1 < PY_SSIZE_T_MAX);
310301
if (list_resize(self, n+1) < 0)
311302
return -1;
312303

@@ -503,8 +494,7 @@ list_concat(PyListObject *a, PyObject *bb)
503494
return NULL;
504495
}
505496
#define b ((PyListObject *)bb)
506-
if (Py_SIZE(a) > PY_SSIZE_T_MAX - Py_SIZE(b))
507-
return PyErr_NoMemory();
497+
assert((size_t)Py_SIZE(a) + (size_t)Py_SIZE(b) < PY_SSIZE_T_MAX);
508498
size = Py_SIZE(a) + Py_SIZE(b);
509499
np = (PyListObject *) list_new_prealloc(size);
510500
if (np == NULL) {

Objects/tupleobject.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -486,8 +486,7 @@ tupleconcat(PyTupleObject *a, PyObject *bb)
486486
Py_INCREF(a);
487487
return (PyObject *)a;
488488
}
489-
if (Py_SIZE(a) > PY_SSIZE_T_MAX - Py_SIZE(b))
490-
return PyErr_NoMemory();
489+
assert((size_t)Py_SIZE(a) + (size_t)Py_SIZE(b) < PY_SSIZE_T_MAX);
491490
size = Py_SIZE(a) + Py_SIZE(b);
492491
if (size == 0) {
493492
return PyTuple_New(0);

0 commit comments

Comments
 (0)
0