Skip to content

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

Merged
merged 1 commit into from
Jul 3, 2020
Merged

bpo-1635741: Fix unicode_dealloc() for mortal interned string #21270

merged 1 commit into from
Jul 3, 2020

Conversation

vstinner
Copy link
Member

@vstinner vstinner commented Jul 1, 2020

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

When unicode_dealloc() is called on a mortal interned string, the
string reference counter is now reset at zero, rather than leaking
one reference.
@vstinner
Copy link
Member Author

vstinner commented Jul 1, 2020

cc @shihai1991 @pablogsal

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) {
Copy link
Member

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?

Copy link
Member Author

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.

Copy link
Member

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.

Copy link
Member

@shihai1991 shihai1991 left a 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 :)

@vstinner vstinner merged commit 3549ca3 into python:master Jul 3, 2020
@vstinner vstinner deleted the unicode_dealloc branch July 3, 2020 14:59
@vstinner
Copy link
Member Author

vstinner commented Jul 3, 2020

Technically, this change is mostly a cleanup, since the object is destroyed anyway a few lines below.

arun-mani-j pushed a commit to arun-mani-j/cpython that referenced this pull request Jul 21, 2020
…GH-21270)

When unicode_dealloc() is called on a mortal interned string, the
string reference counter is now reset at zero.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants