gh-119182: Add checks to PyUnicodeWriter APIs (#120870)

This commit is contained in:
Victor Stinner 2024-06-22 17:25:55 +02:00 committed by GitHub
parent a046c848c1
commit e213475495
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 13 additions and 0 deletions

View File

@ -13347,6 +13347,12 @@ _PyUnicodeWriter_Init(_PyUnicodeWriter *writer)
PyUnicodeWriter* PyUnicodeWriter*
PyUnicodeWriter_Create(Py_ssize_t length) PyUnicodeWriter_Create(Py_ssize_t length)
{ {
if (length < 0) {
PyErr_SetString(PyExc_TypeError,
"length must be positive");
return NULL;
}
const size_t size = sizeof(_PyUnicodeWriter); const size_t size = sizeof(_PyUnicodeWriter);
PyUnicodeWriter *pub_writer = (PyUnicodeWriter *)PyMem_Malloc(size); PyUnicodeWriter *pub_writer = (PyUnicodeWriter *)PyMem_Malloc(size);
if (pub_writer == NULL) { if (pub_writer == NULL) {
@ -13390,6 +13396,7 @@ _PyUnicodeWriter_PrepareInternal(_PyUnicodeWriter *writer,
Py_ssize_t newlen; Py_ssize_t newlen;
PyObject *newbuffer; PyObject *newbuffer;
assert(length >= 0);
assert(maxchar <= MAX_UNICODE); assert(maxchar <= MAX_UNICODE);
/* ensure that the _PyUnicodeWriter_Prepare macro was used */ /* ensure that the _PyUnicodeWriter_Prepare macro was used */
@ -13501,6 +13508,12 @@ _PyUnicodeWriter_WriteChar(_PyUnicodeWriter *writer, Py_UCS4 ch)
int int
PyUnicodeWriter_WriteChar(PyUnicodeWriter *writer, Py_UCS4 ch) PyUnicodeWriter_WriteChar(PyUnicodeWriter *writer, Py_UCS4 ch)
{ {
if (ch > MAX_UNICODE) {
PyErr_SetString(PyExc_ValueError,
"character must be in range(0x110000)");
return -1;
}
return _PyUnicodeWriter_WriteChar((_PyUnicodeWriter*)writer, ch); return _PyUnicodeWriter_WriteChar((_PyUnicodeWriter*)writer, ch);
} }