From 92c3703bdd379cf16723b55f0144cb845f7d3975 Mon Sep 17 00:00:00 2001 From: Timur Mamedov Date: Sat, 20 Jun 2026 19:16:33 -0400 Subject: [PATCH] gh-148268: Fix debug assertion in unsafe_*_compare sort helpers The assert in unsafe_latin_compare, unsafe_long_compare, and unsafe_float_compare assumed PyObject_RichCompareBool always succeeds. It can return -1 on error (e.g. recursion depth), triggering a false assertion failure. Account for the error case. --- ...026-06-20-12-00-00.gh-issue-148268.SrtAsFx.rst | 3 +++ Objects/listobject.c | 15 ++++++++++++--- 2 files changed, 15 insertions(+), 3 deletions(-) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2026-06-20-12-00-00.gh-issue-148268.SrtAsFx.rst diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-06-20-12-00-00.gh-issue-148268.SrtAsFx.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-06-20-12-00-00.gh-issue-148268.SrtAsFx.rst new file mode 100644 index 00000000000000..95f10dee7d542d --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-06-20-12-00-00.gh-issue-148268.SrtAsFx.rst @@ -0,0 +1,3 @@ +Fix debug assertion failure in ``unsafe_latin_compare``, +``unsafe_long_compare``, and ``unsafe_float_compare`` when +:c:func:`PyObject_RichCompareBool` returns ``-1`` on error. diff --git a/Objects/listobject.c b/Objects/listobject.c index 8a9c9bda68269b..aeaede264ccfa8 100644 --- a/Objects/listobject.c +++ b/Objects/listobject.c @@ -2830,7 +2830,10 @@ unsafe_latin_compare(PyObject *v, PyObject *w, MergeState *ms) res < 0 : PyUnicode_GET_LENGTH(v) < PyUnicode_GET_LENGTH(w)); - assert(res == PyObject_RichCompareBool(v, w, Py_LT));; +#ifndef NDEBUG + int cmp = PyObject_RichCompareBool(v, w, Py_LT); + assert(cmp < 0 || res == cmp); +#endif return res; } @@ -2855,7 +2858,10 @@ unsafe_long_compare(PyObject *v, PyObject *w, MergeState *ms) w0 = _PyLong_CompactValue(wl); res = v0 < w0; - assert(res == PyObject_RichCompareBool(v, w, Py_LT)); +#ifndef NDEBUG + int cmp = PyObject_RichCompareBool(v, w, Py_LT); + assert(cmp < 0 || res == cmp); +#endif return res; } @@ -2870,7 +2876,10 @@ unsafe_float_compare(PyObject *v, PyObject *w, MergeState *ms) assert(Py_IS_TYPE(w, &PyFloat_Type)); res = PyFloat_AS_DOUBLE(v) < PyFloat_AS_DOUBLE(w); - assert(res == PyObject_RichCompareBool(v, w, Py_LT)); +#ifndef NDEBUG + int cmp = PyObject_RichCompareBool(v, w, Py_LT); + assert(cmp < 0 || res == cmp); +#endif return res; }