]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Restrict co_code to be under INT_MAX in codeobject (GH-20628)
authorAmmar Askar <ammar@ammaraskar.com>
Wed, 10 Jun 2020 23:31:22 +0000 (23:31 +0000)
committerGitHub <noreply@github.com>
Wed, 10 Jun 2020 23:31:22 +0000 (00:31 +0100)
Objects/codeobject.c
Objects/frameobject.c

index 737635943aced57d3404782d287eb972c6545a9c..cb4fb6812433366c59dadc68805b8e6be2367b19 100644 (file)
@@ -166,6 +166,14 @@ PyCode_NewWithPosOnlyArgs(int argcount, int posonlyargcount, int kwonlyargcount,
         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)) {
index 0dad42ee7bff33c0005268267a78a165843549bb..6e1cbcfaf6f51d9421567d8eee0ead6d5417b3b6 100644 (file)
@@ -397,9 +397,9 @@ frame_setlineno(PyFrameObject *f, PyObject* p_new_lineno, void *Py_UNUSED(ignore
         return -1;
     }
 
-    int len = Py_SAFE_DOWNCAST(
-        PyBytes_GET_SIZE(f->f_code->co_code)/sizeof(_Py_CODEUNIT),
-        Py_ssize_t, int);
+    /* PyCode_NewWithPosOnlyArgs limits co_code to be under INT_MAX so this
+     * should never overflow. */
+    int len = (int)(PyBytes_GET_SIZE(f->f_code->co_code) / sizeof(_Py_CODEUNIT));
     int *lines = marklines(f->f_code, len);
     if (lines == NULL) {
         return -1;