8000 bpo-46323: Fix stack allocation to avoid stackoverflow · python/cpython@3754856 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3754856

Browse files
committed
bpo-46323: Fix stack allocation to avoid stackoverflow
1 parent b552768 commit 3754856

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

Modules/_ctypes/callbacks.c

Lines changed: 8 additions & 9 deletions
< 8000 col width="44"/>
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616
#include <ffi.h>
1717
#include "ctypes.h"
1818

19+
#ifdef HAVE_ALLOCA_H
20+
/* AIX needs alloca.h for alloca() */
21+
#include <alloca.h>
22+
#endif
23+
1924
/**************************************************************/
2025

2126
static void
@@ -155,20 +160,17 @@ static void _CallPythonObject(void *mem,
155160

156161
assert(PyTuple_Check(converters));
157162
nargs = PyTuple_GET_SIZE(converters);
158-
/* Hm. What to return in case of error?
159-
For COM, 0xFFFFFFFF seems better than 0.
160-
*/
161163
if (nargs < 0) {
162164
PrintError("BUG: PySequence_Length");
163165
goto Done;
164166
}
165167

166-
PyObject *args_stack[CTYPES_MAX_ARGCOUNT];
167-
if (nargs <= CTYPES_MAX_ARGCOUNT) {
168+
PyObject *args_stack[_PY_FASTCALL_SMALL_STACK];
169+
if (nargs <= _PY_FASTCALL_SMALL_STACK) {
168170
args = args_stack;
169171
}
170172
else {
171-
args = PyMem_Malloc(nargs * sizeof(PyObject *));
173+
args = (PyObject **)alloca(nargs * sizeof(PyObject *));
172174
if (args == NULL) {
173175
PyErr_NoMemory();
174176
goto Done;
@@ -309,9 +311,6 @@ static void _CallPythonObject(void *mem,
309311
for (j = 0; j < i; j++) {
310312
Py_DECREF(args[j]);
311313
}
312-
if (args != args_stack) {
313-
PyMem_Free(args);
314-
}
315314
PyGILState_Release(state);
316315
}
317316

0 commit comments

Comments
 (0)
0