Skip to content

Commit 7c904e7

Browse files
gh-130567: Fix crash in locale.strxfrm() on macOS
1 parent 4ed046c commit 7c904e7

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

Lib/test/test_locale.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -351,8 +351,7 @@ def setUp(self):
351351
enc = codecs.lookup(locale.getencoding() or 'ascii').name
352352
if enc not in ('utf-8', 'iso8859-1', 'cp1252'):
353353
raise unittest.SkipTest('encoding not suitable')
354-
if enc != 'iso8859-1' and (sys.platform == 'darwin' or is_android or
355-
sys.platform.startswith('freebsd')):
354+
if enc != 'iso8859-1' and is_android:
356355
raise unittest.SkipTest('wcscoll/wcsxfrm have known bugs')
357356
BaseLocalizedTest.setUp(self)
358357

@@ -372,6 +371,21 @@ def test_strcoll_with_diacritic(self):
372371
def test_strxfrm_with_diacritic(self):
373372
self.assertLess(locale.strxfrm('à'), locale.strxfrm('b'))
374373

374+
# @unittest.skipUnless(sys.platform == 'darwin',
375+
# "only macOS")
376+
def test_xxx(self):
377+
bad = []
378+
for c in map(chr, range(1, 0x1000)):
379+
if c.isprintable():
380+
for n in range(8):
381+
x = 'a'*n + c
382+
s = locale.strxfrm(x)
383+
if '\1' in s and s.index('\1') < len(x):
384+
bad += c
385+
print(f'{x!r} {x!a} -> {s!a}')
386+
break
387+
self.fail(repr(''.join(bad)))
388+
375389

376390
class NormalizeTest(unittest.TestCase):
377391
def check(self, localename, expected):

Modules/_localemodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ _locale_strxfrm_impl(PyObject *module, PyObject *str)
457457

458458
/* assume no change in size, first */
459459
n1 = n1 + 1;
460-
buf = PyMem_New(wchar_t, n1);
460+
buf = PyMem_New(wchar_t, n1+1);
461461
if (!buf) {
462462
PyErr_NoMemory();
463463
goto exit;

0 commit comments

Comments
 (0)