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

Skip to content

Commit 3b3b83c

Browse files
authored
Restrict co_code to be under INT_MAX in codeobject (GH-20628)
1 parent 1642c0e commit 3b3b83c

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

Objects/codeobject.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,14 @@ PyCode_NewWithPosOnlyArgs(int argcount, int posonlyargcount, int kwonlyargcount,
166166
return NULL;
167167
}
168168

169+
/* Make sure that code is indexable with an int, this is
170+
a long running assumption in ceval.c and many parts of
171+
the interpreter. */
172+
if (PyBytes_GET_SIZE(code) > INT_MAX) {
173+
PyErr_SetString(PyExc_OverflowError, "co_code larger than INT_MAX");
174+
return NULL;
175+
}
176+
169177
/* Check for any inner or outer closure references */
170178
n_cellvars = PyTuple_GET_SIZE(cellvars);
171179
if (!n_cellvars && !PyTuple_GET_SIZE(freevars)) {

Objects/frameobject.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -397,9 +397,9 @@ frame_setlineno(PyFrameObject *f, PyObject* p_new_lineno, void *Py_UNUSED(ignore
397397
return -1;
398398
}
399399

400-
int len = Py_SAFE_DOWNCAST(
401-
PyBytes_GET_SIZE(f->f_code->co_code)/sizeof(_Py_CODEUNIT),
402-
Py_ssize_t, int);
400+
/* PyCode_NewWithPosOnlyArgs limits co_code to be under INT_MAX so this
401+
* should never overflow. */
402+
int len = (int)(PyBytes_GET_SIZE(f->f_code->co_code) / sizeof(_Py_CODEUNIT));
403403
int *lines = marklines(f->f_code, len);
404404
if (lines == NULL) {
405405
return -1;

0 commit comments

Comments
 (0)