Skip to content

Commit 1331635

Browse files
vstinnerpull[bot]
authored andcommitted
gh-119182: Add checks to PyUnicodeWriter APIs (#120870)
1 parent 7c8b7f8 commit 1331635

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

Objects/unicodeobject.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13347,6 +13347,12 @@ _PyUnicodeWriter_Init(_PyUnicodeWriter *writer)
1334713347
PyUnicodeWriter*
1334813348
PyUnicodeWriter_Create(Py_ssize_t length)
1334913349
{
13350+
if (length < 0) {
13351+
PyErr_SetString(PyExc_TypeError,
13352+
"length must be positive");
13353+
return NULL;
13354+
}
13355+
1335013356
const size_t size = sizeof(_PyUnicodeWriter);
1335113357
PyUnicodeWriter *pub_writer = (PyUnicodeWriter *)PyMem_Malloc(size);
1335213358
if (pub_writer == NULL) {
@@ -13390,6 +13396,7 @@ _PyUnicodeWriter_PrepareInternal(_PyUnicodeWriter *writer,
1339013396
Py_ssize_t newlen;
1339113397
PyObject *newbuffer;
1339213398

13399+
assert(length >= 0);
1339313400
assert(maxchar <= MAX_UNICODE);
1339413401

1339513402
/* ensure that the _PyUnicodeWriter_Prepare macro was used */
@@ -13501,6 +13508,12 @@ _PyUnicodeWriter_WriteChar(_PyUnicodeWriter *writer, Py_UCS4 ch)
1350113508
int
1350213509
PyUnicodeWriter_WriteChar(PyUnicodeWriter *writer, Py_UCS4 ch)
1350313510
{
13511+
if (ch > MAX_UNICODE) {
13512+
PyErr_SetString(PyExc_ValueError,
13513+
"character must be in range(0x110000)");
13514+
return -1;
13515+
}
13516+
1350413517
return _PyUnicodeWriter_WriteChar((_PyUnicodeWriter*)writer, ch);
1350513518
}
1350613519

0 commit comments

Comments
 (0)