summaryrefslogtreecommitdiffstats
path: root/Include/pyport.h
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@python.org>2022-12-07 14:22:38 (GMT)
committerGitHub <noreply@github.com>2022-12-07 14:22:38 (GMT)
commitb11a384dc7471ffc16de4b86e8f5fdeef151f348 (patch)
treec2fc15ebb4de11991f9f1799fa2588a484003073 /Include/pyport.h
parent7031275776f43c76231318c2158a7a2753bc1fba (diff)
downloadcpython-b11a384dc7471ffc16de4b86e8f5fdeef151f348.zip
cpython-b11a384dc7471ffc16de4b86e8f5fdeef151f348.tar.gz
cpython-b11a384dc7471ffc16de4b86e8f5fdeef151f348.tar.bz2
gh-98724: Fix Py_CLEAR() macro side effects (#99100) (#100070)
The Py_CLEAR(), Py_SETREF() and Py_XSETREF() macros now only evaluate their arguments once. If an argument has side effects, these side effects are no longer duplicated. Use temporary variables to avoid duplicating side effects of macro arguments. If available, use _Py_TYPEOF() to avoid type punning. Otherwise, use memcpy() for the assignment to prevent a miscompilation with strict aliasing caused by type punning. Add _Py_TYPEOF() macro: __typeof__() on GCC and clang. Add test_py_clear() and test_py_setref() unit tests to _testcapi.
Diffstat (limited to 'Include/pyport.h')
-rw-r--r--Include/pyport.h9
1 files changed, 9 insertions, 0 deletions
diff --git a/Include/pyport.h b/Include/pyport.h
index b3ff2f4..b1b2a74 100644
--- a/Include/pyport.h
+++ b/Include/pyport.h
@@ -698,6 +698,15 @@ extern char * _getpty(int *, int, mode_t, int);
# define _Py__has_builtin(x) 0
#endif
+// _Py_TYPEOF(expr) gets the type of an expression.
+//
+// Example: _Py_TYPEOF(x) x_copy = (x);
+//
+// The macro is only defined if GCC or clang compiler is used.
+#if defined(__GNUC__) || defined(__clang__)
+# define _Py_TYPEOF(expr) __typeof__(expr)
+#endif
+
/* A convenient way for code to know if sanitizers are enabled. */
#if defined(__has_feature)