New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Macro Py_CLEAR references argument two times. #98724
Comments
|
Here's an example of an expression in cpython/Modules/selectmodule.c Lines 108 to 116 in 365852a
We also have: static int
template_clear(TemplateObject *self)
{
Py_CLEAR(self->literal);
for (Py_ssize_t i = 0, n = Py_SIZE(self); i < n; i++) {
Py_CLEAR(self->items[i].literal);
}
return 0;
}And for (Py_ssize_t i = 0; i < keys->dk_nentries; i++) {
Py_CLEAR(values->values[i]);
}All cases are element access, but this is the most complex examples I am able to find. |
|
I am not sure that I made the point totally clear. Here is an example program for clarification, using some dummy type for This code will give a buffer overflow. Changing the last line to |
|
I'm not sure we've ever promised that macros won't evaluate their args more than once. @vstinner ? |
|
"Duplication of side effects" is one catch of macros that PEP 670 tries to avoid: https://peps.python.org/pep-0670/#rationale Sadly, Py_CLEAR() cannot be converted to a function (as part of PEP 670) since it magically gets a reference to a pointer thanks to magic macro preprocessor. An hypothetical Py_Clear() function would take a pointer to a Python object, so IMO using
I agree with you. Moreover, correctness matters more than performance for Py_CLEAR() API. |
|
Maybe a Somehow related discussions: |
|
I am against extending ABI without need. Py_CLEAR is a part of API, but not in ABI. Incref/decref is anough, Py_CLEAR is just an API sugar. Calling |
|
How about this macro? Replace the free line with the commented out line for real Python, this is for my earlier example program. |
Fix the :c:macro:`Py_CLEAR` macro to avoid the duplicate of side effects. Previously, if the argument had side effects, these side effects were duplicated. Add _testcapi.test_py_clear() unit test.
This issue looks an hypothetical bug, but I'm not convinced that it's possible to write an expression which a side effect and which makes sense to set to NULL. For example, in your example, what's the point of writing To be clear, GCC fails to build the following C code: |
Fix the :c:macro:`Py_CLEAR` macro to avoid the duplicate of side effects. Previously, if the argument had side effects, these side effects were duplicated. Add _testcapi.test_py_clear() unit test.
Fix the Py_CLEAR() macro to avoid the duplicate of side effects. Previously, if the argument had side effects, these side effects were duplicated. Add _testcapi.test_py_clear() unit test.
|
I created PR #99100 to fix this issue. I'm not convinced that we should fix it, but a full PR might help to make a decision. In the past, I saw surprising bug like https://bugs.python.org/issue43181 about passing a C++ expression to a C macro. So well, maybe in case of doubt, it's better to fix the issue to be extra safe. Py_CLEAR() pretends to be safer than using directly the Py_DECREF() macro ;-) |
|
Ah wait, I read again #98724 (comment) and now I got the issue :-) I updated the unit test in my PR #99100. |
|
Perfect! The main problem I have with macros that pretend to be functions is that they might evaluate their arguments several times, and this is totally unexpected. I hope the improved version works for all use-cases :) |
|
I am not sure that it is not just a documentation issue. We can just document that the argument of Py_CLEAR (and the first argument of Py_SETREF) should not have side effect. We can make it working even for arguments with a side effect, but should it be considered a bug fix or a new feature? |
My PR fix the macro so it behaves correctly with arguments with side effects. For me it's a bugfix and should be backported to stable branches. If you are scared of the new implementation (using a pointer to a pointer), I'm fine with only changing the macro in Python 3.12 for now, and only backport if there is a strong pressure from many users to backport the fix. |
|
I am fine with the new implementation. I think all modern compilers can get handle it without adding overhead if it is in a macro. I afraid that users will start to rely on this feature and then their code will work incoretly when compiled with older Python, depending on the bugfix number. |
|
I think this should be extended to all macros, so that they only reference their arguments once. If that can be done it could be made public that from now on macros are safe to use even with arguments that have side effects. |
Fix the Py_CLEAR(), Py_SETREF() and Py_XSETREF() macrox to avoid the duplication of side effects. Previously, if the argument had side effects, these side effects were duplicated. Add test_py_clear() and test_py_setref() unit tests.
|
I completed my PR to fix also Py_SETREF() and Py_XSETREF() macros. |
Fix the Py_CLEAR(), Py_SETREF() and Py_XSETREF() macrox to avoid the duplication of side effects. Previously, if the argument had side effects, these side effects were duplicated. Add test_py_clear() and test_py_setref() unit tests.
Fix the Py_CLEAR(), Py_SETREF() and Py_XSETREF() macrox to avoid the duplication of side effects. Previously, if the argument had side effects, these side effects were duplicated. Add test_py_clear() and test_py_setref() unit tests to _testcapi.
Fix the Py_CLEAR(), Py_SETREF() and Py_XSETREF() macros to avoid the duplication of side effects. Previously, if the argument had side effects, these side effects were duplicated. Add test_py_clear() and test_py_setref() unit tests to _testcapi.
Fix the Py_CLEAR(), Py_SETREF() and Py_XSETREF() macros to avoid the duplication of side effects. Previously, if the argument had side effects, these side effects were duplicated. Add test_py_clear() and test_py_setref() unit tests to _testcapi.
The Py_CLEAR(), Py_SETREF() and Py_XSETREF() macros now only evaluate their argument once. If the argument has side effects, these side effects are no longer duplicated. Add test_py_clear() and test_py_setref() unit tests to _testcapi.
The Py_CLEAR(), Py_SETREF() and Py_XSETREF() macros now only evaluate their argument once. If the argument has side effects, these side effects are no longer duplicated. Add test_py_clear() and test_py_setref() unit tests to _testcapi.
The Py_CLEAR(), Py_SETREF() and Py_XSETREF() macros now only evaluate their argument once. If the argument has side effects, these side effects are no longer duplicated. Add test_py_clear() and test_py_setref() unit tests to _testcapi.
The Py_CLEAR(), Py_SETREF() and Py_XSETREF() macros now only evaluate their argument once. If an argument has side effects, these side effects are no longer duplicated. Add test_py_clear() and test_py_setref() unit tests to _testcapi.
The Py_CLEAR(), Py_SETREF() and Py_XSETREF() macros now only evaluate their argument once. If an argument has side effects, these side effects are no longer duplicated. Add test_py_clear() and test_py_setref() unit tests to _testcapi. (cherry picked from commit c03e05c)
The Py_CLEAR(), Py_SETREF() and Py_XSETREF() macros now only evaluate their argument once. If an argument has side effects, these side effects are no longer duplicated. Add test_py_clear() and test_py_setref() unit tests to _testcapi. (cherry picked from commit c03e05c)
…ythonGH-99288) The Py_CLEAR(), Py_SETREF() and Py_XSETREF() macros now only evaluate their argument once. If an argument has side effects, these side effects are no longer duplicated. Add test_py_clear() and test_py_setref() unit tests to _testcapi. (cherry picked from commit 1082890) Co-authored-by: Victor Stinner <vstinner@python.org> (cherry picked from commit c03e05c)
hochl commentedOct 26, 2022
•
edited by bedevere-bot
Bug report
The macro
Py_CLEAR(op)references the argumentoptwo times. If the macro is called with an expression it will be evaluated two times, for examplePy_CLEAR(p++).Your environment
x86_64
Python 3.7m
Debian Stable
I suggest a fix similar to this (old version commented out with #if 0):
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.
The text was updated successfully, but these errors were encountered: