summaryrefslogtreecommitdiffstats
path: root/Include/object.h
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2015-12-24 08:35:59 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2015-12-24 08:35:59 (GMT)
commit5a57ade58ec5bee85db41b8ce1340ff077781b65 (patch)
tree2f8cf61efba46284b2d4437916bc3469d23c0ce3 /Include/object.h
parenta198645fa0f9a9c6183c211955083765dc8ab3a8 (diff)
downloadcpython-5a57ade58ec5bee85db41b8ce1340ff077781b65.zip
cpython-5a57ade58ec5bee85db41b8ce1340ff077781b65.tar.gz
cpython-5a57ade58ec5bee85db41b8ce1340ff077781b65.tar.bz2
Issue #20440: Massive replacing unsafe attribute setting code with special
macro Py_SETREF.
Diffstat (limited to 'Include/object.h')
-rw-r--r--Include/object.h26
1 files changed, 26 insertions, 0 deletions
diff --git a/Include/object.h b/Include/object.h
index 4d286ef..eac9541 100644
--- a/Include/object.h
+++ b/Include/object.h
@@ -846,6 +846,32 @@ PyAPI_FUNC(void) _Py_Dealloc(PyObject *);
Py_DECREF(_py_xdecref_tmp); \
} while (0)
+#ifndef Py_LIMITED_API
+/* Safely decref `op` and set `op` to `op2`.
+ *
+ * As in case of Py_CLEAR "the obvious" code can be deadly:
+ *
+ * Py_XDECREF(op);
+ * op = op2;
+ *
+ * The safe way is:
+ *
+ * Py_SETREF(op, op2);
+ *
+ * That arranges to set `op` to `op2` _before_ decref'ing, so that any code
+ * triggered as a side-effect of `op` getting torn down no longer believes
+ * `op` points to a valid object.
+ */
+
+#define Py_SETREF(op, op2) \
+ do { \
+ PyObject *_py_tmp = (PyObject *)(op); \
+ (op) = (op2); \
+ Py_XDECREF(_py_tmp); \
+ } while (0)
+
+#endif /* ifndef Py_LIMITED_API */
+
/*
These are provided as conveniences to Python runtime embedders, so that
they can have object code that is not dependent on Python compilation flags.