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
SEGFAULT when setting type.__name__ #60651
Comments
Following script crashes all versions of Python. Cause is the "Py_DECREF(et->ht_name)" in type_set_name(). class Nasty(str):
def __del__(self):
C.__name__ = "other"
class C(object):
pass
C.__name__ = Nasty("abc")
C.__name__ = "normal" |
It looks like the bug is the pattern "Py_DECREF(obj->attr); obj->attr = new_value;". Replacing it with "{ PyObject *tmp = obj->attr; obj->attr = new_value; Py_DECREF(tmp); }" does fix this specific issue. We can use the coccinelle tool to replace all such patterns in the whole CPython code base using attached py_decref_replace.spatch "semantic patch". See also issue bpo-16445, I proposed a similar idea (and another semantic patch). Attached python27_decref_replace.patch patch is the result of the command "spatch -in_place -sp_file py_decref_replace.spatch -dir .". The patch is quite huge, I didn't read it yet :-) Mac/Modules/carbonevt/_CarbonEvtmodule.c | 7 +++++-- |
We should maybe use a macro (ex: Py_DECREC_REPLACE) instead of copying the pattern "{ PyObject *tmp = obj->attr; obj->attr = new_value; Py_DECREF(tmp); }". |
Yes, the macro appropriate here. In Modules/zlibmodule.c this patterns should be fixed by patch for bpo-16350. |
PyObject *tmp = PyUnicode_AsEncodedString(...);
{
PyObject *tmp = s->encoding;
s->encoding = tmp;
Py_DECREF(tmp);
} |
Yes, we should add a "Py_REPLACE()" macro. Sure. +1 to that. With this issue in mind, I wonder if there is any situation where "Py_DECREF/Py_XDECREF" must be used that can not be replace with "Py_CLEAR/Py_REPLACE". Is there any code that breaks if we replace "Py_XDECREF()" by "Py_CLEAR()"?. Could be possible even to replace Py_DECREF definition?. |
Patch for the immediate issue, for Python 2.7. The Py_DECREF is delayed until after setting *both* ht_name and tp_name. |
And the corresponding patch against 3.2 (applies cleanly to 3.3 and default, modulo Misc/NEWS fixes). |
New changeset d5e5017309b1 by Mark Dickinson in branch '2.7': |
New changeset e6d1328412c8 by Mark Dickinson in branch '3.3': New changeset c8d771f10022 by Mark Dickinson in branch 'default': |
Fixed. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: