Skip to content

Commit 1fe8e28

Browse files
committed
fix boundary conditions
1 parent fe0b04e commit 1fe8e28

File tree

3 files changed

+5
-4
lines changed

3 files changed

+5
-4
lines changed

Objects/codeobject.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -445,10 +445,9 @@ _PyCode_Validate(struct _PyCodeConstructor *con)
445445
*
446446
* See https://github.com/python/cpython/issues/126119 for details.
447447
*/
448-
int max_stacksize = (int)(INT_MAX / sizeof(PyObject *))
449-
- FRAME_SPECIALS_SIZE
450-
- (int)PyTuple_GET_SIZE(con->localsplusnames);
451-
if (con->stacksize >= max_stacksize) {
448+
int ub = (int)(INT_MAX / sizeof(PyObject *)) - FRAME_SPECIALS_SIZE;
449+
Py_ssize_t nlocalsplus = PyTuple_GET_SIZE(con->localsplusnames);
450+
if (nlocalsplus >= (Py_ssize_t)ub || con->stacksize >= (int)ub - nlocalsplus) {
452451
PyErr_SetString(PyExc_OverflowError, "code: co_stacksize is too large");
453452
return -1;
454453
}

Objects/frameobject.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1806,6 +1806,7 @@ frame_sizeof(PyFrameObject *f, PyObject *Py_UNUSED(ignored))
18061806
{
18071807
Py_ssize_t base = offsetof(PyFrameObject, _f_frame_data)
18081808
+ offsetof(_PyInterpreterFrame, localsplus);
1809+
assert(base <= INT_MAX);
18091810
PyCodeObject *code = _PyFrame_GetCode(f->f_frame);
18101811
int nslots = _PyFrame_NumSlotsForCodeObject(code);
18111812
assert(nslots >= 0);

Objects/genobject.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -819,6 +819,7 @@ gen_sizeof(PyGenObject *gen, PyObject *Py_UNUSED(ignored))
819819
{
820820
Py_ssize_t base = offsetof(PyGenObject, gi_iframe)
821821
+ offsetof(_PyInterpreterFrame, localsplus);
822+
assert(base <= INT_MAX);
822823
PyCodeObject *code = _PyGen_GetCode(gen);
823824
int nslots = _PyFrame_NumSlotsForCodeObject(code);
824825
assert(nslots >= 0);

0 commit comments

Comments
 (0)