Use Py_ssize_t in _PyEval_EvalCodeWithName()

Issue #27128, #18295: replace int type with Py_ssize_t for index variables used
for positional arguments. It should help to avoid integer overflow and help to
emit better machine code for "i++" (no trap needed for overflow).

Make also the total_args variable constant.
This commit is contained in:
Victor Stinner 2016-08-16 23:39:42 +02:00
parent c70200165c
commit 17061a99b0
1 changed files with 10 additions and 8 deletions

View File

@ -3795,8 +3795,8 @@ _PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
PyObject **fastlocals, **freevars; PyObject **fastlocals, **freevars;
PyThreadState *tstate; PyThreadState *tstate;
PyObject *x, *u; PyObject *x, *u;
int total_args = co->co_argcount + co->co_kwonlyargcount; const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
int i, n; Py_ssize_t i, n;
PyObject *kwdict; PyObject *kwdict;
assert((kwcount == 0) || (kws != NULL)); assert((kwcount == 0) || (kws != NULL));
@ -3864,7 +3864,7 @@ _PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
PyObject **co_varnames; PyObject **co_varnames;
PyObject *keyword = kws[2*i]; PyObject *keyword = kws[2*i];
PyObject *value = kws[2*i + 1]; PyObject *value = kws[2*i + 1];
int j; Py_ssize_t j;
if (keyword == NULL || !PyUnicode_Check(keyword)) { if (keyword == NULL || !PyUnicode_Check(keyword)) {
PyErr_Format(PyExc_TypeError, PyErr_Format(PyExc_TypeError,
@ -3928,11 +3928,13 @@ _PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
/* Add missing positional arguments (copy default values from defs) */ /* Add missing positional arguments (copy default values from defs) */
if (argcount < co->co_argcount) { if (argcount < co->co_argcount) {
int m = co->co_argcount - defcount; Py_ssize_t m = co->co_argcount - defcount;
int missing = 0; Py_ssize_t missing = 0;
for (i = argcount; i < m; i++) for (i = argcount; i < m; i++) {
if (GETLOCAL(i) == NULL) if (GETLOCAL(i) == NULL) {
missing++; missing++;
}
}
if (missing) { if (missing) {
missing_arguments(co, missing, defcount, fastlocals); missing_arguments(co, missing, defcount, fastlocals);
goto fail; goto fail;
@ -3952,7 +3954,7 @@ _PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
/* Add missing keyword arguments (copy default values from kwdefs) */ /* Add missing keyword arguments (copy default values from kwdefs) */
if (co->co_kwonlyargcount > 0) { if (co->co_kwonlyargcount > 0) {
int missing = 0; Py_ssize_t missing = 0;
for (i = co->co_argcount; i < total_args; i++) { for (i = co->co_argcount; i < total_args; i++) {
PyObject *name; PyObject *name;
if (GETLOCAL(i) != NULL) if (GETLOCAL(i) != NULL)