Skip to content

gh-98724: Fix type punning issue in Py_SETREF() - #99739

Closed
vstinner wants to merge 1 commit into
python:mainfrom
vstinner:setref_typeof
Closed

vstinner wants to merge 1 commit into
python:mainfrom
vstinner:setref_typeof

Conversation

@vstinner

@vstinner vstinner commented Nov 23, 2022 •

Copy link
Copy Markdown
Member

Fix type punning issue in Py_CLEAR(), Py_SETREF() and Py_XSETREF() macros. Don't cast pointers to avoid type punning which cause miscompilation of functions using these macros.

Fix type punning issue in Py_CLEAR(), Py_SETREF() and Py_XSETREF()
macros. Don't cast pointers to avoid type punning which cause
miscompilation of functions using these macros.
@vstinner

Copy link
Copy Markdown
Member Author
#define _PY_TYPEOF(EXPR) __typeof__(EXPR)

On GCC and clang, typeof() can maybe used. In "GNU mode", typeof is treated as a keyword by the C compiler.

On MSVC, if __typeof__() doesn't work, maybe decltype() can be used instead.

@vstinner

Copy link
Copy Markdown
Member Author

Ah, building Python with the PR fails on Windows (MSVC) with:

warning C4013: '__typeof__' undefined
@vstinner

Copy link
Copy Markdown
Member Author

Since MSVC doesn't implement __typeof__(), I don't think that this approach works. I would like the C API to behave the same on all platforms: see #98724 (comment) I close my PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

2 participants