summaryrefslogtreecommitdiff
path: root/Objects/codeobject.c
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2018-07-16 00:09:44 -0700
committerGitHub <noreply@github.com>2018-07-16 00:09:44 -0700
commit5594f1dfbe214151b75405be042e9420a3649241 (patch)
treea045a3c525469b2ac9874cde9480fbf2955d4286 /Objects/codeobject.c
parent3fc12f0e2ad9ba49243f5a43a8803018b2ff5904 (diff)
downloadcpython-git-5594f1dfbe214151b75405be042e9420a3649241.tar.gz
bpo-24618: Add a check in the code constructor. (GH-8283)
Check that the size of the varnames tuple is enough at least for all arguments. (cherry picked from commit bd47384e07bde38a8f18b90b4cea02a505d95c75) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Diffstat (limited to 'Objects/codeobject.c')
-rw-r--r--Objects/codeobject.c18
1 files changed, 15 insertions, 3 deletions
diff --git a/Objects/codeobject.c b/Objects/codeobject.c
index 0509b8e640..7ef012585a 100644
--- a/Objects/codeobject.c
+++ b/Objects/codeobject.c
@@ -103,7 +103,7 @@ PyCode_New(int argcount, int kwonlyargcount,
{
PyCodeObject *co;
Py_ssize_t *cell2arg = NULL;
- Py_ssize_t i, n_cellvars;
+ Py_ssize_t i, n_cellvars, n_varnames, total_args;
/* Check argument types */
if (argcount < 0 || kwonlyargcount < 0 || nlocals < 0 ||
@@ -138,10 +138,22 @@ PyCode_New(int argcount, int kwonlyargcount,
flags &= ~CO_NOFREE;
}
+ n_varnames = PyTuple_GET_SIZE(varnames);
+ if (argcount <= n_varnames && kwonlyargcount <= n_varnames) {
+ /* Never overflows. */
+ total_args = (Py_ssize_t)argcount + (Py_ssize_t)kwonlyargcount +
+ ((flags & CO_VARARGS) != 0) + ((flags & CO_VARKEYWORDS) != 0);
+ }
+ else {
+ total_args = n_varnames + 1;
+ }
+ if (total_args > n_varnames) {
+ PyErr_SetString(PyExc_ValueError, "code: varnames is too small");
+ return NULL;
+ }
+
/* Create mapping between cells and arguments if needed. */
if (n_cellvars) {
- Py_ssize_t total_args = argcount + kwonlyargcount +
- ((flags & CO_VARARGS) != 0) + ((flags & CO_VARKEYWORDS) != 0);
bool used_cell2arg = false;
cell2arg = PyMem_NEW(Py_ssize_t, n_cellvars);
if (cell2arg == NULL) {