From a9b583dd270a0a02c846f95ac918d7c774eaa31c Mon Sep 17 00:00:00 2001 From: Gouri Jain Date: Wed, 25 Feb 2026 14:32:21 +0530 Subject: [PATCH] gh-145202: Fix segfault in unicodedata.iter_graphemes during GC --- .../Library/2026-02-25-14-30-00.gh-issue-145202.xR3f9A.rst | 1 + Modules/unicodedata.c | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2026-02-25-14-30-00.gh-issue-145202.xR3f9A.rst diff --git a/Misc/NEWS.d/next/Library/2026-02-25-14-30-00.gh-issue-145202.xR3f9A.rst b/Misc/NEWS.d/next/Library/2026-02-25-14-30-00.gh-issue-145202.xR3f9A.rst new file mode 100644 index 00000000000000..25e4ca33c241e3 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-02-25-14-30-00.gh-issue-145202.xR3f9A.rst @@ -0,0 +1 @@ +Fix a segmentation fault in :func:`unicodedata.iter_graphemes` when the iterator is deallocated after being cleared by the garbage collector. diff --git a/Modules/unicodedata.c b/Modules/unicodedata.c index 401f64e7416944..b8f3da72eb910a 100644 --- a/Modules/unicodedata.c +++ b/Modules/unicodedata.c @@ -1913,7 +1913,7 @@ Segment_dealloc(PyObject *self) { PyTypeObject *tp = Py_TYPE(self); PyObject_GC_UnTrack(self); - Py_DECREF(((SegmentObject *)self)->string); + Py_XDECREF(((SegmentObject *)self)->string); tp->tp_free(self); Py_DECREF(tp); } @@ -1989,7 +1989,7 @@ GBI_dealloc(PyObject *self) { PyTypeObject *tp = Py_TYPE(self); PyObject_GC_UnTrack(self); - Py_DECREF(((GraphemeBreakIterator *)self)->iter.str); + Py_XDECREF(((GraphemeBreakIterator *)self)->iter.str); tp->tp_free(self); Py_DECREF(tp); }