[3.15] gh-154176: Fix locale.strxfrm() crash on DragonFly BSD (GH-154177) (GH-154217)

serhiy-storchaka <[email protected]>
Newsgroups gmane.comp.python.cvs
Message-ID <[email protected]>
https://github.com/python/cpython/commit/2e4a1fa41dab372af40aa886079b14d61db42c98
commit: 2e4a1fa41dab372af40aa886079b14d61db42c98
branch: 3.15
author: Miss Islington (bot) <[email protected]>
committer: serhiy-storchaka <[email protected]>
date: 2026-07-20T11:15:34+03:00
summary:

[3.15] gh-154176: Fix locale.strxfrm() crash on DragonFly BSD (GH-154177) (GH-154217)

Query the result size with a real one-element buffer instead of
wcsxfrm(NULL, s, 0): DragonFly BSD's wcsxfrm() crashes when the
destination is NULL or the size is 0.
(cherry picked from commit f389b03f9f2768342bfd49987a257460d4aa4faf)

Co-authored-by: Serhiy Storchaka <[email protected]>
Co-authored-by: Claude Fable 5 <[email protected]>

files:
A Misc/NEWS.d/next/Library/2026-07-19-21-30-00.gh-issue-154176.strxfrm.rst
M Modules/_localemodule.c

diff --git a/Misc/NEWS.d/next/Library/2026-07-19-21-30-00.gh-issue-154176.strxfrm.rst b/Misc/NEWS.d/next/Library/2026-07-19-21-30-00.gh-issue-154176.strxfrm.rst
new file mode 100644
index 00000000000000..9ffa5828000113
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2026-07-19-21-30-00.gh-issue-154176.strxfrm.rst
@@ -0,0 +1,2 @@
+Fix a crash in :func:`locale.strxfrm` on DragonFly BSD,
+whose ``wcsxfrm()`` does not support the zero size.
diff --git a/Modules/_localemodule.c b/Modules/_localemodule.c
index 8f7d662b00b21b..35fbcd00045939 100644
--- a/Modules/_localemodule.c
+++ b/Modules/_localemodule.c
@@ -443,6 +443,7 @@ _locale_strxfrm_impl(PyObject *module, PyObject *str)
 {
     Py_ssize_t n1;
     wchar_t *s = NULL, *buf = NULL;
+    wchar_t dummy[1];
     size_t n2;
     PyObject *result = NULL;
 
@@ -456,7 +457,9 @@ _locale_strxfrm_impl(PyObject *module, PyObject *str)
     }
 
     errno = 0;
-    n2 = wcsxfrm(NULL, s, 0);
+    /* Query the size with a real one-element buffer: DragonFly BSD's
+       wcsxfrm() crashes when the destination is NULL or the size is 0. */
+    n2 = wcsxfrm(dummy, s, 1);
     if (errno && errno != ERANGE) {
         PyErr_SetFromErrno(PyExc_OSError);
         goto exit;

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.