Skip to content

Macro Py_CLEAR references argument two times. #98724

Description

@hochl

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-core(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions