Skip to content

Commit cd6cd54

Browse files
Copilotcsm10495
andcommitted
Address review: safer overflow check for slicelength_bound + 1
Co-authored-by: csm10495 <5749838+csm10495@users.noreply.github.com> Agent-Logs-Url: https://github.com/csm10495/cpython/sessions/996cabbe-7dc6-4faa-b95b-31907ef41b20
1 parent 646671a commit cd6cd54

1 file changed

Lines changed: 12 additions & 6 deletions

File tree

Objects/listobject.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3850,12 +3850,18 @@ list_ass_subscript_lock_held(PyObject *_self, PyObject *item, PyObject *value)
38503850
}
38513851
return -1;
38523852
}
3853-
Py_ssize_t alloc = slicelength_bound + 1;
3854-
if (alloc <= 0) {
3855-
/* Overflow or zero-length slice; still collect at
3856-
least 1 item so the size check can detect a
3857-
non-empty iterable. */
3858-
alloc = 1;
3853+
Py_ssize_t alloc;
3854+
if (slicelength_bound >= PY_SSIZE_T_MAX) {
3855+
alloc = PY_SSIZE_T_MAX;
3856+
}
3857+
else {
3858+
alloc = slicelength_bound + 1;
3859+
if (alloc <= 0) {
3860+
/* Zero-length slice; still collect at least
3861+
1 item so the size check can detect a
3862+
non-empty iterable. */
3863+
alloc = 1;
3864+
}
38593865
}
38603866
seq = PyList_New(alloc);
38613867
if (seq == NULL) {

0 commit comments

Comments
 (0)