summaryrefslogtreecommitdiffstats
path: root/Include
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@python.org>2022-05-17 17:43:19 (GMT)
committerGitHub <noreply@github.com>2022-05-17 17:43:19 (GMT)
commit13058323621bfb1776fc39948a4096c76f8cbc81 (patch)
tree32b9923eac5b2d6bd6e2ce12c0296a2374eb542f /Include
parente6fd7992a92879103215b3e9f218fe07212af9b1 (diff)
downloadcpython-13058323621bfb1776fc39948a4096c76f8cbc81.zip
cpython-13058323621bfb1776fc39948a4096c76f8cbc81.tar.gz
cpython-13058323621bfb1776fc39948a4096c76f8cbc81.tar.bz2
gh-89653: Add assertions on PyUnicode_READ() index (#92883)
Add assertions on the index argument of PyUnicode_READ(), PyUnicode_READ_CHAR() and PyUnicode_WRITE() functions.
Diffstat (limited to 'Include')
-rw-r--r--Include/cpython/unicodeobject.h10
1 files changed, 9 insertions, 1 deletions
diff --git a/Include/cpython/unicodeobject.h b/Include/cpython/unicodeobject.h
index f853b69..758aaff 100644
--- a/Include/cpython/unicodeobject.h
+++ b/Include/cpython/unicodeobject.h
@@ -304,6 +304,7 @@ static inline Py_ssize_t PyUnicode_GET_LENGTH(PyObject *op) {
static inline void PyUnicode_WRITE(int kind, void *data,
Py_ssize_t index, Py_UCS4 value)
{
+ assert(index >= 0);
if (kind == PyUnicode_1BYTE_KIND) {
assert(value <= 0xffU);
_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,
static inline Py_UCS4 PyUnicode_READ(int kind,
const void *data, Py_ssize_t index)
{
+ assert(index >= 0);
if (kind == PyUnicode_1BYTE_KIND) {
return _Py_STATIC_CAST(const Py_UCS1*, data)[index];
}
@@ -351,7 +353,13 @@ static inline Py_UCS4 PyUnicode_READ(int kind,
cache kind and use PyUnicode_READ instead. */
static inline Py_UCS4 PyUnicode_READ_CHAR(PyObject *unicode, Py_ssize_t index)
{
- int kind = PyUnicode_KIND(unicode);
+ int kind;
+
+ assert(index >= 0);
+ // Tolerate reading the NUL character at str[len(str)]
+ assert(index <= PyUnicode_GET_LENGTH(unicode));
+
+ kind = PyUnicode_KIND(unicode);
if (kind == PyUnicode_1BYTE_KIND) {
return PyUnicode_1BYTE_DATA(unicode)[index];
}