Skip to content

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

Merged
vstinner merged 1 commit into
python:masterfrom
vstinner:unicode_dealloc
Jul 3, 2020
Merged

bpo-1635741: Fix unicode_dealloc() for mortal interned string#21270
vstinner merged 1 commit into
python:masterfrom
vstinner:unicode_dealloc

Conversation

@vstinner

@vstinner vstinner commented Jul 1, 2020

Copy link
Copy Markdown
Member

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

vstinner commented Jul 1, 2020

Copy link
Copy Markdown
Member Author
Comment thread Objects/unicodeobject.c
PyDict_DelItem(). */
assert(Py_REFCNT(unicode) == 0);
Py_SET_REFCNT(unicode, 3);
if (PyDict_DelItem(interned, unicode) != 0) {

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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.

@shihai1991 shihai1991 left a comment

Copy link
Copy Markdown
Member

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

vstinner commented Jul 3, 2020

Copy link
Copy Markdown
Member Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

4 participants