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

Skip to content

Commit db73376

Browse files
authored
[3.9] bpo-43499: Restrict co_code to be under INT_MAX in codeobject (GH-20628) (GH-24896)
(cherry picked from commit 3b3b83c)
1 parent 1f0cde6 commit db73376

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,9 @@ frame_setlineno(PyFrameObject *f, PyObject* p_new_lineno, void *Py_UNUSED(ignore
397397
return -1;
398398
}
399399

400-
int len = PyBytes_GET_SIZE(f->f_code->co_code)/sizeof(_Py_CODEUNIT);
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));
401403
int *lines = marklines(f->f_code, len);
402404
if (lines == NULL) {
403405
return -1;

0 commit comments

Comments
 (0)