Restrict co_code to be under INT_MAX in codeobject (GH-20628)
diff --git a/Objects/codeobject.c b/Objects/codeobject.c
index 7376359..cb4fb68 100644
--- a/Objects/codeobject.c
+++ b/Objects/codeobject.c
@@ -166,6 +166,14 @@
return NULL;
}
+ /* Make sure that code is indexable with an int, this is
+ a long running assumption in ceval.c and many parts of
+ the interpreter. */
+ if (PyBytes_GET_SIZE(code) > INT_MAX) {
+ PyErr_SetString(PyExc_OverflowError, "co_code larger than INT_MAX");
+ return NULL;
+ }
+
/* Check for any inner or outer closure references */
n_cellvars = PyTuple_GET_SIZE(cellvars);
if (!n_cellvars && !PyTuple_GET_SIZE(freevars)) {