-
-
Notifications
You must be signed in to change notification settings - Fork 31.9k
bpo-1635741: Fix unicode_dealloc() for mortal interned string #21270
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
Conversation
When unicode_dealloc() is called on a mortal interned string, the string reference counter is now reset at zero, rather than leaking one reference.
to prevent calling unicode_dealloc() again. Adjust refcnt after | ||
PyDict_DelItem(). */ | ||
assert(Py_REFCNT(unicode) == 0); | ||
Py_SET_REFCNT(unicode, 3); | ||
if (PyDict_DelItem(interned, unicode) != 0) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we skip PyDict_DelItem()
and set interned unicode's refcnt = 0 directly in here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We cannot remove the PyDict_DelItem() call.
PyUnicode_InternInPlace() uses black magic: it ignores 2 references (key and value) of the interned dictionary.
We must remove the string from the interned dictionary. Otherwise, the dictionary will contain a dangling pointer when unicode_dealloc() completes: using the dict is likely to crash in this case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
got it, make sense, thanks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks victor's PR, LGTM :)
Technically, this change is mostly a cleanup, since the object is destroyed anyway a few lines below. |
…GH-21270) When unicode_dealloc() is called on a mortal interned string, the string reference counter is now reset at zero.
When unicode_dealloc() is called on a mortal interned string, the
string reference counter is now reset at zero, rather than leaking
one reference.
https://bugs.python.org/issue1635741