Skip to content
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
2 changes: 1 addition & 1 deletion Doc/c-api/unicode.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1896,7 +1896,7 @@ object.

.. c:function:: int PyUnicodeWriter_WriteUCS4(PyUnicodeWriter *writer, const Py_UCS4 *str, Py_ssize_t size)

Writer the UCS4 string *str* into *writer*.
Write the UCS4 string *str* into *writer*.

*size* is a number of UCS4 characters.

Expand Down
54 changes: 39 additions & 15 deletions Lib/test/test_capi/test_unicode.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
from _testcapi import PY_SSIZE_T_MIN, PY_SSIZE_T_MAX, SIZEOF_WCHAR_T


MAX_UNICODE = _testinternalcapi._Py_MAX_UNICODE
# The first invalid character after MAX_UNICODE
INVALID_CHAR = MAX_UNICODE + 1
# Maximum invalid character which fits into 32-bit Py_UCS4
MAX_INVALID_CHAR = 0xFFFF_FFFF
NULL = None

class Str(str):
Expand Down Expand Up @@ -73,14 +78,14 @@ def test_new(self):
self.assertEqual(new(0, maxchar), '')
self.assertEqual(new(5, maxchar), chr(maxchar)*5)
self.assertRaises(MemoryError, new, PY_SSIZE_T_MAX, maxchar)
self.assertEqual(new(0, 0x110000), '')
self.assertEqual(new(0, INVALID_CHAR), '')
self.assertRaises(MemoryError, new, PY_SSIZE_T_MAX//2, 0x4f60)
self.assertRaises(MemoryError, new, PY_SSIZE_T_MAX//2+1, 0x4f60)
self.assertRaises(MemoryError, new, PY_SSIZE_T_MAX//2, 0x1f600)
self.assertRaises(MemoryError, new, PY_SSIZE_T_MAX//2+1, 0x1f600)
self.assertRaises(MemoryError, new, PY_SSIZE_T_MAX//4, 0x1f600)
self.assertRaises(MemoryError, new, PY_SSIZE_T_MAX//4+1, 0x1f600)
self.assertRaises(SystemError, new, 5, 0x110000)
self.assertRaises(SystemError, new, 5, INVALID_CHAR)
self.assertRaises(SystemError, new, -1, 0)
self.assertRaises(SystemError, new, PY_SSIZE_T_MIN, 0)

Expand Down Expand Up @@ -115,7 +120,7 @@ def test_fill(self):
s = strings[0]
self.assertRaises(IndexError, fill, s, -1, 0, 0x78)
self.assertRaises(IndexError, fill, s, PY_SSIZE_T_MIN, 0, 0x78)
self.assertRaises(ValueError, fill, s, 0, 0, 0x110000)
self.assertRaises(ValueError, fill, s, 0, 0, INVALID_CHAR)
self.assertRaises(SystemError, fill, b'abc', 0, 0, 0x78)
self.assertRaises(SystemError, fill, [], 0, 0, 0x78)
# CRASHES fill(s, 0, NULL, 0, 0)
Expand All @@ -129,7 +134,7 @@ def _test_writechar(self, writechar, *, check):
'\U0001f600\U0001f601\U0001f602'
]
# one character for every kind + out of range code
chars = [0x78, 0xa9, 0x20ac, 0x1f638, 0x110000]
chars = [0x78, 0xa9, 0x20ac, 0x1f638, INVALID_CHAR]
for i, s in enumerate(strings):
for j, c in enumerate(chars):
if j <= i:
Expand Down Expand Up @@ -301,7 +306,17 @@ def test_fromkindanddata(self):
self.assertRaises(ValueError, fromkindanddata, 1, NULL, -1)
self.assertRaises(ValueError, fromkindanddata, 1, NULL, PY_SSIZE_T_MIN)
# CRASHES fromkindanddata(1, NULL, 1)
# CRASHES fromkindanddata(4, b'\xff\xff\xff\xff')

# Test invalid UCS-4 string
for invalid_char in (INVALID_CHAR, MAX_INVALID_CHAR):
with self.subTest(invalid_char=invalid_char):
# Test single character
ucs4_char = invalid_char.to_bytes(4, byteorder=sys.byteorder)
self.assertRaises(ValueError, fromkindanddata, 4, ucs4_char)

# Test multiple characters
s = 'valid'.encode(enc4) + ucs4_char
self.assertRaises(ValueError, fromkindanddata, 4, s)

def test_substring(self):
"""Test PyUnicode_Substring()"""
Expand Down Expand Up @@ -446,7 +461,7 @@ def check_format(expected, format, *args):
check_format('\U0010ffff',
b'%c', c_int(0x10ffff))
with self.assertRaises(OverflowError):
PyUnicode_FromFormat(b'%c', c_int(0x110000))
PyUnicode_FromFormat(b'%c', c_int(INVALID_CHAR))
# Issue #18183
check_format('\U00010000\U00100000',
b'%c%c', c_int(0x10000), c_int(0x100000))
Expand Down Expand Up @@ -1015,7 +1030,7 @@ def test_fromordinal(self):
self.assertEqual(fromordinal(0x20ac), '\u20ac')
self.assertEqual(fromordinal(0x1f600), '\U0001f600')

self.assertRaises(ValueError, fromordinal, 0x110000)
self.assertRaises(ValueError, fromordinal, INVALID_CHAR)
self.assertRaises(ValueError, fromordinal, -1)

def test_asutf8(self):
Expand Down Expand Up @@ -1367,8 +1382,8 @@ def test_findchar(self):
self.assertEqual(unicode_findchar(str, ord(ch), 0, len(str), -1), i)

str = "!>_<!"
self.assertEqual(unicode_findchar(str, 0x110000, 0, len(str), 1), -1)
self.assertEqual(unicode_findchar(str, 0x110000, 0, len(str), -1), -1)
self.assertEqual(unicode_findchar(str, INVALID_CHAR, 0, len(str), 1), -1)
self.assertEqual(unicode_findchar(str, INVALID_CHAR, 0, len(str), -1), -1)
# start < end
self.assertEqual(unicode_findchar(str, ord('!'), 1, len(str)+1, 1), 4)
self.assertEqual(unicode_findchar(str, ord('!'), 1, PY_SSIZE_T_MAX, 1), 4)
Expand Down Expand Up @@ -1757,7 +1772,7 @@ def test_max_char_value(self):
self.assertEqual(max_char_value('ascii'), 0x7f)
self.assertEqual(max_char_value('latin1:\xe9'), 0xff)
self.assertEqual(max_char_value('bmp:\u20ac'), 0xffff)
self.assertEqual(max_char_value('\U0010ffff'), 0x10_ffff)
self.assertEqual(max_char_value(chr(0x10_0000)), 0x10_ffff)

# CRASHES max_char_value(NULL)

Expand Down Expand Up @@ -1935,8 +1950,8 @@ def test_write_char(self):
writer.write_char(ord('$'))
writer.write_char(0x20ac)
writer.write_char(0x10_ffff)
self.assertRaises(ValueError, writer.write_char, 0x11_0000)
self.assertRaises(ValueError, writer.write_char, 0xFFFF_FFFF)
self.assertRaises(ValueError, writer.write_char, INVALID_CHAR)
self.assertRaises(ValueError, writer.write_char, MAX_INVALID_CHAR)
self.assertEqual(writer.finish(),
"\0$\u20AC\U0010FFFF")

Expand Down Expand Up @@ -2106,11 +2121,20 @@ def test_ucs4(self):
writer.write_ucs4("pair\uD83D\uDC0D".encode(encoding, 'surrogatepass'))
writer.write_char(ord("-"))
writer.write_ucs4("null[\0]".encode(encoding), 7)
invalid = (b'\x00\x00\x11\x00' if sys.byteorder == 'little' else
b'\x00\x11\x00\x00')
# CRASHES writer.write_ucs4("invalid".encode(encoding) + invalid)
writer.write_ucs4(NULL, 0)
# CRASHES writer.write_ucs4(NULL, 1)

# Invalid UCS-4 strings
for invalid_char in (INVALID_CHAR, MAX_INVALID_CHAR):
with self.subTest(invalid_char=invalid_char):
# Test single character
ucs4_char = invalid_char.to_bytes(4, byteorder=sys.byteorder)
self.assertRaises(ValueError, writer.write_ucs4, ucs4_char)

# Test multiple characters
s = 'valid'.encode(encoding) + ucs4_char
self.assertRaises(ValueError, writer.write_ucs4, s)

self.assertEqual(writer.finish(),
"lone\udc80-pair\ud83d\udc0d-null[\x00]")

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
:c:func:`PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND)
<PyUnicode_FromKindAndData>` and :c:func:`PyUnicodeWriter_WriteUCS4` now raise
an exception if a character is not in the [U+0000; U+10ffff] range, instead of
creating an invalid str object. Patch by Victor Stinner.
4 changes: 4 additions & 0 deletions Modules/_testinternalcapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -3544,6 +3544,10 @@ module_exec(PyObject *module)
}
PyModule_AddObject(module, "SelfInterruptingContextManager", (PyObject *)&SelfInterruptingContextManager_Type);

if (PyModule_AddIntMacro(module, _Py_MAX_UNICODE) < 0) {
return 1;
}

return 0;
}

Expand Down
48 changes: 40 additions & 8 deletions Objects/unicodeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1592,6 +1592,14 @@ PyUnicode_CopyCharacters(PyObject *to, Py_ssize_t to_start,
return how_many;
}

static void
unicode_invalid_character(Py_UCS4 ch)
{
PyErr_Format(PyExc_ValueError,
"character U+%x is not in range [U+0000; U+%x]",
ch, MAX_UNICODE);
}

/* Find the maximum code point and count the number of surrogate pairs so a
correct string length can be computed before converting a string to UCS4.
This function counts single surrogates as a character and not as a pair.
Expand Down Expand Up @@ -1627,9 +1635,7 @@ find_maxchar_surrogates(const wchar_t *begin, const wchar_t *end,
if (ch > *maxchar) {
*maxchar = ch;
if (*maxchar > MAX_UNICODE) {
PyErr_Format(PyExc_ValueError,
"character U+%x is not in range [U+0000; U+%x]",
ch, MAX_UNICODE);
unicode_invalid_character(ch);
return -1;
}
}
Expand Down Expand Up @@ -2224,15 +2230,29 @@ static PyObject*
_PyUnicode_FromUCS4(const Py_UCS4 *u, Py_ssize_t size)
{
PyObject *res;
Py_UCS4 max_char;

if (size == 0)
_Py_RETURN_UNICODE_EMPTY();
assert(size > 0);
if (size == 1)

// ucs4lib_find_max_char() cannot be used, it ignores limit greater
// than MAX_UNICODE
Py_UCS4 max_char = 127;
for (Py_ssize_t i = 0; i < size; i++) {
Py_UCS4 ch = u[i];
if (ch > max_char) {
if (ch > MAX_UNICODE) {
unicode_invalid_character(ch);
return NULL;
}
max_char = ch;
}
}

if (size == 1) {
return unicode_char(u[0]);
}

max_char = ucs4lib_find_max_char(u, u + size);
res = PyUnicode_New(size, max_char);
if (!res)
return NULL;
Expand Down Expand Up @@ -2266,9 +2286,21 @@ PyUnicodeWriter_WriteUCS4(PyUnicodeWriter *pub_writer,
return 0;
}

Py_UCS4 max_char = ucs4lib_find_max_char(str, str + size);
// ucs4lib_find_max_char() cannot be used, it ignores limit greater
// than MAX_UNICODE
Py_UCS4 maxchar = 127;
for (Py_ssize_t i = 0; i < size; i++) {
Py_UCS4 ch = str[i];
if (ch > maxchar) {
if (ch > MAX_UNICODE) {
unicode_invalid_character(ch);
return -1;
}
maxchar = ch;
}
}

if (_PyUnicodeWriter_Prepare(writer, size, max_char) < 0) {
if (_PyUnicodeWriter_Prepare(writer, size, maxchar) < 0) {
return -1;
}
assert(_PyUnicodeWriter_CanWrite(writer));
Expand Down
Loading