Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-89653: Add assertions to unicodeobject.h functions #91800

Merged
merged 1 commit into from
Apr 21, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Include/cpython/unicodeobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ static inline void* _PyUnicode_COMPACT_DATA(PyObject *op) {
}

static inline void* _PyUnicode_NONCOMPACT_DATA(PyObject *op) {
assert(!PyUnicode_IS_COMPACT(op));
void *data = _PyUnicodeObject_CAST(op)->data.any;
assert(data != NULL);
return data;
Expand Down Expand Up @@ -354,13 +355,16 @@ static inline void PyUnicode_WRITE(unsigned int kind, void *data,
Py_ssize_t index, Py_UCS4 value)
{
if (kind == PyUnicode_1BYTE_KIND) {
assert(value <= 0xffU);
((Py_UCS1 *)data)[index] = (Py_UCS1)value;
}
else if (kind == PyUnicode_2BYTE_KIND) {
assert(value <= 0xffffU);
((Py_UCS2 *)data)[index] = (Py_UCS2)value;
}
else {
assert(kind == PyUnicode_4BYTE_KIND);
assert(value <= 0x10ffffU);
((Py_UCS4 *)data)[index] = value;
}
}
Expand All @@ -378,6 +382,7 @@ static inline Py_UCS4 PyUnicode_READ(unsigned int kind,
if (kind == PyUnicode_2BYTE_KIND) {
return ((const Py_UCS2 *)data)[index];
}
assert(kind == PyUnicode_4BYTE_KIND);
return ((const Py_UCS4 *)data)[index];
}
#define PyUnicode_READ(kind, data, index) \
Expand All @@ -397,6 +402,7 @@ static inline Py_UCS4 PyUnicode_READ_CHAR(PyObject *unicode, Py_ssize_t index)
if (kind == PyUnicode_2BYTE_KIND) {
return PyUnicode_2BYTE_DATA(unicode)[index];
}
assert(kind == PyUnicode_4BYTE_KIND);
return PyUnicode_4BYTE_DATA(unicode)[index];
}
#define PyUnicode_READ_CHAR(unicode, index) \
Expand All @@ -419,6 +425,7 @@ static inline Py_UCS4 PyUnicode_MAX_CHAR_VALUE(PyObject *op)
if (kind == PyUnicode_2BYTE_KIND) {
return 0xffffU;
}
assert(kind == PyUnicode_4BYTE_KIND);
return 0x10ffffU;
}
#define PyUnicode_MAX_CHAR_VALUE(op) \
Expand Down