Skip to content

Commit efe7fd4

Browse files
authored
gh-89653: Add assertions to unicodeobject.h functions (#91800)
1 parent 636ad7b commit efe7fd4

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

Include/cpython/unicodeobject.h

+7
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,7 @@ static inline void* _PyUnicode_COMPACT_DATA(PyObject *op) {
314314
}
315315

316316
static inline void* _PyUnicode_NONCOMPACT_DATA(PyObject *op) {
317+
assert(!PyUnicode_IS_COMPACT(op));
317318
void *data = _PyUnicodeObject_CAST(op)->data.any;
318319
assert(data != NULL);
319320
return data;
@@ -354,13 +355,16 @@ static inline void PyUnicode_WRITE(unsigned int kind, void *data,
354355
Py_ssize_t index, Py_UCS4 value)
355356
{
356357
if (kind == PyUnicode_1BYTE_KIND) {
358+
assert(value <= 0xffU);
357359
((Py_UCS1 *)data)[index] = (Py_UCS1)value;
358360
}
359361
else if (kind == PyUnicode_2BYTE_KIND) {
362+
assert(value <= 0xffffU);
360363
((Py_UCS2 *)data)[index] = (Py_UCS2)value;
361364
}
362365
else {
363366
assert(kind == PyUnicode_4BYTE_KIND);
367+
assert(value <= 0x10ffffU);
364368
((Py_UCS4 *)data)[index] = value;
365369
}
366370
}
@@ -378,6 +382,7 @@ static inline Py_UCS4 PyUnicode_READ(unsigned int kind,
378382
if (kind == PyUnicode_2BYTE_KIND) {
379383
return ((const Py_UCS2 *)data)[index];
380384
}
385+
assert(kind == PyUnicode_4BYTE_KIND);
381386
return ((const Py_UCS4 *)data)[index];
382387
}
383388
#define PyUnicode_READ(kind, data, index) \
@@ -397,6 +402,7 @@ static inline Py_UCS4 PyUnicode_READ_CHAR(PyObject *unicode, Py_ssize_t index)
397402
if (kind == PyUnicode_2BYTE_KIND) {
398403
return PyUnicode_2BYTE_DATA(unicode)[index];
399404
}
405+
assert(kind == PyUnicode_4BYTE_KIND);
400406
return PyUnicode_4BYTE_DATA(unicode)[index];
401407
}
402408
#define PyUnicode_READ_CHAR(unicode, index) \
@@ -419,6 +425,7 @@ static inline Py_UCS4 PyUnicode_MAX_CHAR_VALUE(PyObject *op)
419425
if (kind == PyUnicode_2BYTE_KIND) {
420426
return 0xffffU;
421427
}
428+
assert(kind == PyUnicode_4BYTE_KIND);
422429
return 0x10ffffU;
423430
}
424431
#define PyUnicode_MAX_CHAR_VALUE(op) \

0 commit comments

Comments
 (0)