diff options
author | Victor Stinner <vstinner@python.org> | 2024-06-22 15:25:55 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-06-22 15:25:55 (GMT) |
commit | e21347549535b16f51a39986b78a2c2cd4ed09f4 (patch) | |
tree | b2eecebbd420ec653ae5be691f3b212e8e14af3a /Objects | |
parent | a046c848c1df0cf98092e9696594d3fb836e3530 (diff) | |
download | cpython-e21347549535b16f51a39986b78a2c2cd4ed09f4.zip cpython-e21347549535b16f51a39986b78a2c2cd4ed09f4.tar.gz cpython-e21347549535b16f51a39986b78a2c2cd4ed09f4.tar.bz2 |
gh-119182: Add checks to PyUnicodeWriter APIs (#120870)
Diffstat (limited to 'Objects')
-rw-r--r-- | Objects/unicodeobject.c | 13 |
1 files changed, 13 insertions, 0 deletions
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index 4c174cb..279cdaa 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -13347,6 +13347,12 @@ _PyUnicodeWriter_Init(_PyUnicodeWriter *writer) PyUnicodeWriter* 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); PyUnicodeWriter *pub_writer = (PyUnicodeWriter *)PyMem_Malloc(size); if (pub_writer == NULL) { @@ -13390,6 +13396,7 @@ _PyUnicodeWriter_PrepareInternal(_PyUnicodeWriter *writer, Py_ssize_t newlen; PyObject *newbuffer; + assert(length >= 0); assert(maxchar <= MAX_UNICODE); /* ensure that the _PyUnicodeWriter_Prepare macro was used */ @@ -13501,6 +13508,12 @@ _PyUnicodeWriter_WriteChar(_PyUnicodeWriter *writer, Py_UCS4 ch) int 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); } |