Skip to content

gh-130567: fix strxfrm memory allocation #130619

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Lib/test/test_locale.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,17 @@ def test_strcoll_with_diacritic(self):
def test_strxfrm_with_diacritic(self):
self.assertLess(locale.strxfrm('à'), locale.strxfrm('b'))

@unittest.skipIf(sys.platform.startswith('aix'),
'bpo-29972: broken test on AIX')
@unittest.skipIf(
is_emscripten or is_wasi,
"musl libc issue on Emscripten/WASI, bpo-46390"
)
@unittest.skipIf(sys.platform.startswith("netbsd"),
"gh-124108: NetBSD doesn't support UTF-8 for LC_COLLATE")
def test_strxfrm_non_latin_1(self):
self.assertLess(locale.strxfrm('s'), locale.strxfrm('š'))


class NormalizeTest(unittest.TestCase):
def check(self, localename, expected):
Expand Down
55 changes: 32 additions & 23 deletions Modules/_localemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -394,49 +394,58 @@ static PyObject *
_locale_strxfrm_impl(PyObject *module, PyObject *str)
/*[clinic end generated code: output=3081866ebffc01af input=1378bbe6a88b4780]*/
{
Py_ssize_t n1;
Py_ssize_t buf_len;
wchar_t *s = NULL, *buf = NULL;
size_t n2;
size_t xfrm_result;
PyObject *result = NULL;

s = PyUnicode_AsWideCharString(str, &n1);
s = PyUnicode_AsWideCharString(str, &buf_len);
if (s == NULL)
goto exit;
if (wcslen(s) != (size_t)n1) {
if (wcslen(s) != (size_t)buf_len) {
PyErr_SetString(PyExc_ValueError,
"embedded null character");
goto exit;
}

/* assume no change in size, first */
n1 = n1 + 1;
buf = PyMem_New(wchar_t, n1);
buf_len = buf_len + 1;
buf = PyMem_New(wchar_t, buf_len);
if (!buf) {
PyErr_NoMemory();
goto exit;
}
errno = 0;
n2 = wcsxfrm(buf, s, n1);
if (errno && errno != ERANGE) {
PyErr_SetFromErrno(PyExc_OSError);
goto exit;
}
if (n2 >= (size_t)n1) {
/* more space needed */
wchar_t * new_buf = PyMem_Realloc(buf, (n2+1)*sizeof(wchar_t));
for (;;) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not comfortable with unbound loops :-( I prefer the current code which calls wcsxfrm() once or twice, but no more.

errno = 0;
xfrm_result = wcsxfrm(buf, s, buf_len);
if (errno && errno != ERANGE) {
PyErr_SetFromErrno(PyExc_OSError);
break;
}

if (xfrm_result < (size_t)buf_len) {
// wcsxfrm succeeded, return result
result = PyUnicode_FromWideChar(buf, xfrm_result);
break;
}

if (xfrm_result > (size_t)buf_len) {
// Assume this is desired buffer size
buf_len = xfrm_result + 1;
} else {
// Some platforms, such as macOS 15 doesn't return desired buffer
// size so it is up to the caller to figure out needed buffer size
// (gh-130567).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should check for integer overflow, something like:

            if (buf_len > PY_SSIZE_T_MAX / 2) {
                PyErr_NoMemory();
                break;
            }

buf_len = buf_len * 2;
}

wchar_t * new_buf = PyMem_Realloc(buf, buf_len * sizeof(wchar_t));
if (!new_buf) {
PyErr_NoMemory();
goto exit;
break;
}
buf = new_buf;
errno = 0;
n2 = wcsxfrm(buf, s, n2+1);
if (errno) {
PyErr_SetFromErrno(PyExc_OSError);
goto exit;
}
}
result = PyUnicode_FromWideChar(buf, n2);
exit:
PyMem_Free(buf);
PyMem_Free(s);
Expand Down
Loading