[Python-Dev] Py_CLEAR to avoid crashes

Neil Schemenauer nas at arctrix.com
Mon Feb 18 22:52:14 CET 2008


On Mon, Feb 18, 2008 at 05:48:57PM +0100, Amaury Forgeot d'Arc wrote:
> For example, in exception.c, BaseException_init() starts with the instruction:
>     Py_DECREF(self->args);
> this may call __del__ on self->args

Ah, I understand now.  We are not talking about tp_dealloc methods
(the GC takes great pains to avoid this scenario).  However, any
object that calls Py_DECREF outside of its tp_dealloc method must be
prepared for finalizers to access it in arbitrary ways.

That sucks.  Most Py_DECREF calls are probably okay but it's going
to be hard to find the ones that are not.  I can't think of anything
we can do to make this trap harder to fall into.  Even using
Py_CLEAR as a blunt tool is not a total solution. You could still
end up with a null pointer dereference if the code is not written
carefully.

  Neil


More information about the Python-Dev mailing list