Skip to content

Commit c9cd047

Browse files
Apply Victor's suggestions, and actually fix the bug
Co-authored-by: vstinner@python.org
1 parent 0517659 commit c9cd047

2 files changed

Lines changed: 20 additions & 2 deletions

File tree

‎Modules/_remote_debugging/object_reading.c‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
******************************************************************************/
77

88
#include "_remote_debugging.h"
9+
#include "internal/pycore_unicodeobject.h" // _Py_MAX_UNICODE
910
#include <limits.h>
1011

1112
/* ============================================================================
@@ -153,6 +154,22 @@ read_py_str(
153154
}
154155
}
155156

157+
if (kind == PyUnicode_4BYTE_KIND) {
158+
Py_UCS4 *ucs4 = (Py_UCS4 *)data;
159+
for (Py_ssize_t i = 0; i < len; i++) {
160+
Py_UCS4 ch = ucs4[i];
161+
if (ch > _Py_MAX_UNICODE) {
162+
PyMem_Free(data);
163+
PyErr_Format(PyExc_ValueError,
164+
"character U+%x is not in range [U+0000; U+%x]",
165+
ch, _Py_MAX_UNICODE);
166+
set_exception_cause(unwinder, PyExc_ValueError,
167+
"Unicode string contains invalid character");
168+
return NULL;
169+
}
170+
}
171+
}
172+
156173
PyObject *result = PyUnicode_FromKindAndData(kind, data, len);
157174
PyMem_Free(data);
158175
if (result == NULL) {

‎Modules/_testinternalcapi.c‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2056,15 +2056,16 @@ unicode_transformdecimalandspacetoascii(PyObject *self, PyObject *arg)
20562056
return _PyUnicode_TransformDecimalAndSpaceToASCII(arg);
20572057
}
20582058

2059-
/* Return a one-character str holding a code point above U+10FFFF. */
2059+
/* Return a str holding code points above U+10FFFF. */
20602060
static PyObject *
20612061
new_invalid_str(PyObject *self, PyObject *Py_UNUSED(ignored))
20622062
{
2063-
PyObject *str = PyUnicode_New(1, 0x10FFFF);
2063+
PyObject *str = PyUnicode_New(2, 0x10FFFF);
20642064
if (str == NULL) {
20652065
return NULL;
20662066
}
20672067
PyUnicode_4BYTE_DATA(str)[0] = 0x110000;
2068+
PyUnicode_4BYTE_DATA(str)[1] = 0x110000;
20682069
return str;
20692070
}
20702071

0 commit comments

Comments
 (0)