Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c9590ad

Browse files
committed
Close #14085: remove assertions from PyUnicode_WRITE macro
Add checks in PyUnicode_WriteChar() and convert PyUnicode_New() assertion to a test raising a Python exception.
1 parent d263d18 commit c9590ad

2 files changed

Lines changed: 10 additions & 4 deletions

File tree

Include/unicodeobject.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -499,17 +499,14 @@ enum PyUnicode_Kind {
499499
do { \
500500
switch ((kind)) { \
501501
case PyUnicode_1BYTE_KIND: { \
502-
assert(value <= 0xff); \
503502
((Py_UCS1 *)(data))[(index)] = (Py_UCS1)(value); \
504503
break; \
505504
} \
506505
case PyUnicode_2BYTE_KIND: { \
507-
assert(value <= 0xffff); \
508506
((Py_UCS2 *)(data))[(index)] = (Py_UCS2)(value); \
509507
break; \
510508
} \
511509
default: { \
512-
assert(value <= 0x10ffff); \
513510
assert((kind) == PyUnicode_4BYTE_KIND); \
514511
((Py_UCS4 *)(data))[(index)] = (Py_UCS4)(value); \
515512
} \

Objects/unicodeobject.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -998,7 +998,11 @@ PyUnicode_New(Py_ssize_t size, Py_UCS4 maxchar)
998998
is_sharing = 1;
999999
}
10001000
else {
1001-
assert(maxchar <= MAX_UNICODE);
1001+
if (maxchar > MAX_UNICODE) {
1002+
PyErr_SetString(PyExc_SystemError,
1003+
"invalid maximum character passed to PyUnicode_New");
1004+
return NULL;
1005+
}
10021006
kind_state = PyUnicode_4BYTE_KIND;
10031007
char_size = 4;
10041008
if (sizeof(wchar_t) == 4)
@@ -3931,6 +3935,7 @@ PyUnicode_ReadChar(PyObject *unicode, Py_ssize_t index)
39313935
int
39323936
PyUnicode_WriteChar(PyObject *unicode, Py_ssize_t index, Py_UCS4 ch)
39333937
{
3938+
Py_UCS4 maxchar;
39343939
if (!PyUnicode_Check(unicode) || !PyUnicode_IS_COMPACT(unicode)) {
39353940
PyErr_BadArgument();
39363941
return -1;
@@ -3942,6 +3947,10 @@ PyUnicode_WriteChar(PyObject *unicode, Py_ssize_t index, Py_UCS4 ch)
39423947
}
39433948
if (unicode_check_modifiable(unicode))
39443949
return -1;
3950+
if (ch > PyUnicode_MAX_CHAR_VALUE(unicode)) {
3951+
PyErr_SetString(PyExc_ValueError, "character out of range");
3952+
return -1;
3953+
}
39453954
PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode),
39463955
index, ch);
39473956
return 0;

0 commit comments

Comments
 (0)