Macro Py_CLEAR references argument two times. · Issue #98724 · python/cpython (original) (raw)
Bug report
The macro Py_CLEAR(op)
references the argument op
two times. If the macro is called with an expression it will be evaluated two times, for example Py_CLEAR(p++)
.
Your environment
x86_64
- CPython versions tested on:
Python 3.7m - Operating system and architecture:
Debian Stable
I suggest a fix similar to this (old version commented out with #if 0):
#if 0
#define Py_CLEAR(op) \
do { \
PyObject *_py_tmp = (PyObject *)(op); \
if (_py_tmp != NULL) { \
(op) = NULL; \
Py_DECREF(_py_tmp); \
} \
} while (0)
#else
#define Py_CLEAR(op) \
do { \
PyObject **_py_tmp = (PyObject **)&(op); \
if (*_py_tmp != NULL) { \
PyObject *_py_tmp2 = *_py_tmp; \
(*_py_tmp) = NULL; \
Py_DECREF(_py_tmp2); \
} \
} while (0)
#endif
I am not sure if this has happened anywhere, but I see a possible problem here. I think the compiler will optimize out the additional temporary variable in most cases.
- PR: gh-98724: Fix Py_CLEAR() macro side effects #99100
- PR: [3.11] gh-98724: Fix Py_CLEAR() macro side effects (#99100) #99288
- PR: [3.10] gh-98724: Fix Py_CLEAR() macro side effects (GH-99100) (GH-99288) #99292
- PR: [3.11] Revert "[3.11] gh-98724: Fix Py_CLEAR() macro side effects (#99100)" #99573
- PR: Revert "gh-98724: Fix Py_CLEAR() macro side effects" #99737
- PR: gh-98724: Fix type punning issue in Py_SETREF() #99739
- PR: gh-98724: Fix warnings on Py_SETREF() usage #99781
- PR: gh-98724: Fix Py_CLEAR() macro side effects (#99100) #100070
- PR: gh-98724: Fix the Py_CLEAR() macro in the limited C API #100121