Skip to content

gh-145202: Fix segfault in unicodedata.iter_graphemes when cleared by GC#145203

Closed
gourijain029-del wants to merge 1 commit intopython:mainfrom
gourijain029-del:fix-unicodedata-iter-graphemes-segfault
Closed

gh-145202: Fix segfault in unicodedata.iter_graphemes when cleared by GC#145203
gourijain029-del wants to merge 1 commit intopython:mainfrom
gourijain029-del:fix-unicodedata-iter-graphemes-segfault

Conversation

@gourijain029-del
Copy link

This PR fixes a segmentation fault in unicodedata.iter_graphemes and its segment objects, which occurred when the objects were cleared by the garbage collector before being deallocated.

In Modules/unicodedata.c, both GraphemeBreakIterator and SegmentObject have tp_clear implementations that set their string references to NULL. However, their deallocators were using Py_DECREF instead of Py_XDECREF. This led to a crash if a GC cycle (or interpreter shutdown) cleared the objects before they reached the deallocation path.

I've updated GBI_dealloc and Segment_dealloc to use Py_XDECREF to safely handle these NULL references.

Fixes gh-145202.

@serhiy-storchaka
Copy link
Member

#145216 is an alternative solution. I do not think we need tp_clear here at first place.

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.

Segfault from unicodedata.iter_graphemes on a free threaded build

2 participants