@@ -304,6 +304,7 @@ static inline Py_ssize_t PyUnicode_GET_LENGTH(PyObject *op) {
304
304
static inline void PyUnicode_WRITE (int kind , void * data ,
305
305
Py_ssize_t index , Py_UCS4 value )
306
306
{
307
+ assert (index >= 0 );
307
308
if (kind == PyUnicode_1BYTE_KIND ) {
308
309
assert (value <= 0xffU );
309
310
_Py_STATIC_CAST (Py_UCS1 * , data ) [index ] = _Py_STATIC_CAST (Py_UCS1 , value );
@@ -329,6 +330,7 @@ static inline void PyUnicode_WRITE(int kind, void *data,
329
330
static inline Py_UCS4 PyUnicode_READ (int kind ,
330
331
const void * data , Py_ssize_t index )
331
332
{
333
+ assert (index >= 0 );
332
334
if (kind == PyUnicode_1BYTE_KIND ) {
333
335
return _Py_STATIC_CAST (const Py_UCS1 * , data )[index ];
334
336
}
@@ -351,7 +353,13 @@ static inline Py_UCS4 PyUnicode_READ(int kind,
351
353
cache kind and use PyUnicode_READ instead. */
352
354
static inline Py_UCS4 PyUnicode_READ_CHAR (PyObject * unicode , Py_ssize_t index )
353
355
{
354
- int kind = PyUnicode_KIND (unicode );
356
+ int kind ;
357
+
358
+ assert (index >= 0 );
359
+ // Tolerate reading the NUL character at str[len(str)]
360
+ assert (index <= PyUnicode_GET_LENGTH (unicode ));
361
+
362
+ kind = PyUnicode_KIND (unicode );
355
363
if (kind == PyUnicode_1BYTE_KIND ) {
356
364
return PyUnicode_1BYTE_DATA (unicode )[index ];
357
365
}
0 commit comments