From b3f28d48169d6e91131eacb2d83759009499edfc Mon Sep 17 00:00:00 2001 From: Max Bachmann <kontakt@maxbachmann.de> Date: Tue, 30 Mar 2021 22:09:00 +0200 Subject: [PATCH] Fix type conversions in vectorcallfunc (GH-4054) --- Cython/Utility/CythonFunction.c | 2 +- Cython/Utility/ModuleSetupCode.c | 6 +++--- Cython/Utility/ObjectHandling.c | 27 ++++++++++++++------------- 3 files changed, 18 insertions(+), 17 deletions(-) diff --git a/Cython/Utility/CythonFunction.c b/Cython/Utility/CythonFunction.c index 29d5c1de7..69e325e12 100644 --- a/Cython/Utility/CythonFunction.c +++ b/Cython/Utility/CythonFunction.c @@ -680,7 +680,7 @@ static PyObject *__Pyx_CyFunction_CallAsMethod(PyObject *func, PyObject *args, P __pyx_vectorcallfunc vc = __Pyx_CyFunction_func_vectorcall(cyfunc); if (vc) { #if CYTHON_ASSUME_SAFE_MACROS - return __Pyx_PyVectorcall_FastCallDict(func, vc, &PyTuple_GET_ITEM(args, 0), PyTuple_GET_SIZE(args), kw); + return __Pyx_PyVectorcall_FastCallDict(func, vc, &PyTuple_GET_ITEM(args, 0), (size_t)PyTuple_GET_SIZE(args), kw); #else // avoid unused function warning (void) &__Pyx_PyVectorcall_FastCallDict; diff --git a/Cython/Utility/ModuleSetupCode.c b/Cython/Utility/ModuleSetupCode.c index 0116d4b26..9d6c0e48a 100644 --- a/Cython/Utility/ModuleSetupCode.c +++ b/Cython/Utility/ModuleSetupCode.c @@ -553,15 +553,15 @@ class __Pyx_FakeReference { #if CYTHON_VECTORCALL #define __pyx_vectorcallfunc vectorcallfunc #define __Pyx_PY_VECTORCALL_ARGUMENTS_OFFSET PY_VECTORCALL_ARGUMENTS_OFFSET - #define __Pyx_PyVectorcall_NARGS(n) PyVectorcall_NARGS(n) + #define __Pyx_PyVectorcall_NARGS(n) PyVectorcall_NARGS((size_t)(n)) #elif CYTHON_BACKPORT_VECTORCALL typedef PyObject *(*__pyx_vectorcallfunc)(PyObject *callable, PyObject *const *args, size_t nargsf, PyObject *kwnames); #define __Pyx_PY_VECTORCALL_ARGUMENTS_OFFSET ((size_t)1 << (8 * sizeof(size_t) - 1)) - #define __Pyx_PyVectorcall_NARGS(n) ((n) & ~__Pyx_PY_VECTORCALL_ARGUMENTS_OFFSET) + #define __Pyx_PyVectorcall_NARGS(n) ((Py_ssize_t)(((size_t)(n)) & ~__Pyx_PY_VECTORCALL_ARGUMENTS_OFFSET)) #else #define __Pyx_PY_VECTORCALL_ARGUMENTS_OFFSET 0 - #define __Pyx_PyVectorcall_NARGS(n) (n) + #define __Pyx_PyVectorcall_NARGS(n) ((Py_ssize_t)(n)) #endif #if CYTHON_COMPILING_IN_PYPY && !defined(PyObject_Malloc) diff --git a/Cython/Utility/ObjectHandling.c b/Cython/Utility/ObjectHandling.c index f26d062e2..bd8dd7aa9 100644 --- a/Cython/Utility/ObjectHandling.c +++ b/Cython/Utility/ObjectHandling.c @@ -2103,7 +2103,7 @@ bad: /////////////// PyObjectFastCall.proto /////////////// #define __Pyx_PyObject_FastCall(func, args, nargs) __Pyx_PyObject_FastCallDict(func, args, nargs, NULL) -static CYTHON_INLINE PyObject* __Pyx_PyObject_FastCallDict(PyObject *func, PyObject **args, Py_ssize_t nargs, PyObject *kwargs); /*proto*/ +static CYTHON_INLINE PyObject* __Pyx_PyObject_FastCallDict(PyObject *func, PyObject **args, size_t nargs, PyObject *kwargs); /*proto*/ /////////////// PyObjectFastCall /////////////// //@requires: PyObjectCall @@ -2111,23 +2111,23 @@ static CYTHON_INLINE PyObject* __Pyx_PyObject_FastCallDict(PyObject *func, PyObj //@requires: PyObjectCallMethO //@substitute: naming -static PyObject* __Pyx_PyObject_FastCall_fallback(PyObject *func, PyObject **args, Py_ssize_t nargs, PyObject *kwargs) { +static PyObject* __Pyx_PyObject_FastCall_fallback(PyObject *func, PyObject **args, size_t nargs, PyObject *kwargs) { PyObject *argstuple; PyObject *result; - Py_ssize_t i; + size_t i; argstuple = PyTuple_New(nargs); if (unlikely(!argstuple)) return NULL; for (i = 0; i < nargs; i++) { Py_INCREF(args[i]); - PyTuple_SET_ITEM(argstuple, i, args[i]); + PyTuple_SET_ITEM(argstuple, (Py_ssize_t)i, args[i]); } result = __Pyx_PyObject_Call(func, argstuple, kwargs); Py_DECREF(argstuple); return result; } -static CYTHON_INLINE PyObject* __Pyx_PyObject_FastCallDict(PyObject *func, PyObject **args, Py_ssize_t _nargs, PyObject *kwargs) { +static CYTHON_INLINE PyObject* __Pyx_PyObject_FastCallDict(PyObject *func, PyObject **args, size_t _nargs, PyObject *kwargs) { // Special fast paths for 0 and 1 arguments // NOTE: in many cases, this is called with a constant value for nargs // which is known at compile-time. So the branches below will typically @@ -2181,20 +2181,20 @@ static CYTHON_INLINE PyObject* __Pyx_PyObject_FastCallDict(PyObject *func, PyObj #if CYTHON_VECTORCALL vectorcallfunc f = _PyVectorcall_Function(func); if (f) { - return f(func, args, nargs, kwargs); + return f(func, args, (size_t)nargs, kwargs); } #elif defined(__Pyx_CyFunction_USED) && CYTHON_BACKPORT_VECTORCALL // exclude fused functions for now if (__Pyx_CyFunction_CheckExact(func)) { __pyx_vectorcallfunc f = __Pyx_CyFunction_func_vectorcall(func); - if (f) return f(func, args, nargs, kwargs); + if (f) return f(func, args, (size_t)nargs, kwargs); } #endif if (nargs == 0) { return __Pyx_PyObject_Call(func, $empty_tuple, kwargs); } - return __Pyx_PyObject_FastCall_fallback(func, args, nargs, kwargs); + return __Pyx_PyObject_FastCall_fallback(func, args, (size_t)nargs, kwargs); } @@ -2559,14 +2559,14 @@ static CYTHON_INLINE PyObject* __Pyx_PyObject_CallNoArg(PyObject *func) { /////////////// PyVectorcallFastCallDict.proto /////////////// #if CYTHON_METH_FASTCALL -static CYTHON_INLINE PyObject *__Pyx_PyVectorcall_FastCallDict(PyObject *func, __pyx_vectorcallfunc vc, PyObject *const *args, Py_ssize_t nargs, PyObject *kw); +static CYTHON_INLINE PyObject *__Pyx_PyVectorcall_FastCallDict(PyObject *func, __pyx_vectorcallfunc vc, PyObject *const *args, size_t nargs, PyObject *kw); #endif /////////////// PyVectorcallFastCallDict /////////////// #if CYTHON_METH_FASTCALL // Slow path when kw is non-empty -static PyObject *__Pyx_PyVectorcall_FastCallDict_kw(PyObject *func, __pyx_vectorcallfunc vc, PyObject *const *args, Py_ssize_t nargs, PyObject *kw) +static PyObject *__Pyx_PyVectorcall_FastCallDict_kw(PyObject *func, __pyx_vectorcallfunc vc, PyObject *const *args, size_t nargs, PyObject *kw) { // Code based on _PyObject_FastCallDict() and _PyStack_UnpackDict() from CPython PyObject *res = NULL; @@ -2574,17 +2574,18 @@ static PyObject *__Pyx_PyVectorcall_FastCallDict_kw(PyObject *func, __pyx_vector PyObject **newargs; PyObject **kwvalues; Py_ssize_t i, pos; + size_t j; PyObject *key, *value; unsigned long keys_are_strings; Py_ssize_t nkw = PyDict_GET_SIZE(kw); // Copy positional arguments - newargs = (PyObject **)PyMem_Malloc((nargs + nkw) * sizeof(args[0])); + newargs = (PyObject **)PyMem_Malloc((nargs + (size_t)nkw) * sizeof(args[0])); if (unlikely(newargs == NULL)) { PyErr_NoMemory(); return NULL; } - for (i = 0; i < nargs; i++) newargs[i] = args[i]; + for (j = 0; j < nargs; j++) newargs[j] = args[j]; // Copy keyword arguments kwnames = PyTuple_New(nkw); @@ -2619,7 +2620,7 @@ cleanup: return res; } -static CYTHON_INLINE PyObject *__Pyx_PyVectorcall_FastCallDict(PyObject *func, __pyx_vectorcallfunc vc, PyObject *const *args, Py_ssize_t nargs, PyObject *kw) +static CYTHON_INLINE PyObject *__Pyx_PyVectorcall_FastCallDict(PyObject *func, __pyx_vectorcallfunc vc, PyObject *const *args, size_t nargs, PyObject *kw) { if (likely(kw == NULL) || PyDict_GET_SIZE(kw) == 0) { return vc(func, args, nargs, NULL); -- 2.30.9