[issue13097] ctypes: segfault with large number of callback arguments

Meador Inge report at bugs.python.org
Thu Dec 1 05:24:09 CET 2011


Meador Inge <meadori at gmail.com> added the comment:

On Wed, Nov 30, 2011 at 6:20 AM, Amaury Forgeot d'Arc
<report at bugs.python.org> wrote:

> Right, alloca() could be replaced by some malloc(), but is it really useful?  After all, when a C function calls back to Python, all arguments needs to be > pushed to the stack anyway.

The case is somewhat pathological.  However, there are *four* 'alloca'
calls in '_ctypes_callproc', three of which are proportional to
'argcount'.  And as you point out there is an additional stack
allocation inside of 'libffi' when the callback is actually called
(also using 'alloca').

I see two reasons switching to 'malloc' might be beneficial: (1) by
shifting some of the allocation to dynamic allocation we may reduce
the chance that we blow the stack at all.  Keep in mind that this gets
more likely if the C ffi function is called from a deep in a call tree
and *not* necessarily with just a huge number of parameters.  (2) if
resources are exhausted, then we can exit more gracefully.  Out of
dynamic memory errors can actually be handled as opposed to an
'alloca' call that ends up allocating more than is left.

That being said, if this does get changed it is low priority.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue13097>
_______________________________________


More information about the Python-bugs-list mailing list