Skip to content

Commit 2e4a1fa

Browse files
miss-islingtonserhiy-storchakaclaude
authored
[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 f389b03) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com> Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
1 parent 346c7af commit 2e4a1fa

2 files changed

Lines changed: 6 additions & 1 deletion

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix a crash in :func:`locale.strxfrm` on DragonFly BSD,
2+
whose ``wcsxfrm()`` does not support the zero size.

Modules/_localemodule.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,7 @@ _locale_strxfrm_impl(PyObject *module, PyObject *str)
443443
{
444444
Py_ssize_t n1;
445445
wchar_t *s = NULL, *buf = NULL;
446+
wchar_t dummy[1];
446447
size_t n2;
447448
PyObject *result = NULL;
448449

@@ -456,7 +457,9 @@ _locale_strxfrm_impl(PyObject *module, PyObject *str)
456457
}
457458

458459
errno = 0;
459-
n2 = wcsxfrm(NULL, s, 0);
460+
/* Query the size with a real one-element buffer: DragonFly BSD's
461+
wcsxfrm() crashes when the destination is NULL or the size is 0. */
462+
n2 = wcsxfrm(dummy, s, 1);
460463
if (errno && errno != ERANGE) {
461464
PyErr_SetFromErrno(PyExc_OSError);
462465
goto exit;

0 commit comments

Comments
 (0)