Title: Py_CLEAR(tmp) seg faults
Type: crash Stage:
Components: Interpreter Core Versions: Python 3.0, Python 2.5
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: alexandre.vassalotti, ncoghlan, stutzbach
Priority: normal Keywords:

Created on 2008-07-03 17:54 by stutzbach, last changed 2008-07-13 20:43 by alexandre.vassalotti. This issue is now closed.

Messages (4)
msg69213 - (view) Author: Daniel Stutzbach (stutzbach) (Python committer) Date: 2008-07-03 17:54
I'm writing a C extension module and discovered that Py_CLEAR() causes a
crash if the programmer happened to name their variable "tmp".  The
Py_CLEAR() macro internally uses the name "tmp" in a new scope, hiding
the callers "tmp", and calling Py_DECREF() on an essentially random bit
of memory.

I suggest changing Py_CLEAR() to use something a little less common than
"tmp".  Perhaps "_py_tmp".

For easy reference, here's how Py_CLEAR() is defined now:

#define Py_CLEAR(op)				\
        do {                            	\
                if (op) {			\
                        PyObject *tmp = (PyObject *)(op);	\
                        (op) = NULL;		\
                        Py_DECREF(tmp);		\
                }				\
        } while (0)
msg69498 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2008-07-10 10:11
A better option may be to append _tmp to the passed in token:

#define Py_CLEAR(op)				\
        do {                            	\
                if (op) {			\
                        PyObject *op##_tmp = (PyObject *)(op);	\
                        (op) = NULL;		\
                        Py_DECREF(op##_tmp);		\
                }				\
        } while (0)
msg69500 - (view) Author: Daniel Stutzbach (stutzbach) (Python committer) Date: 2008-07-10 12:24
Appending _tmp is a good idea, but it won't work when the parameter
isn't a simple symbol.  For example, there's a line in cPickle.c like
this: Py_CLEAR(*p).
msg69621 - (view) Author: Alexandre Vassalotti (alexandre.vassalotti) * (Python committer) Date: 2008-07-13 20:43
Committed the fix r64927. Thanks.
Date User Action Args
2008-07-13 20:43:46alexandre.vassalottisetstatus: open -> closed
resolution: fixed
messages: + msg69621
nosy: + alexandre.vassalotti
2008-07-10 12:24:55stutzbachsetmessages: + msg69500
2008-07-10 10:11:07ncoghlansetnosy: + ncoghlan
messages: + msg69498
2008-07-03 17:54:08stutzbachcreate