diff options
author | Victor Stinner <victor.stinner@gmail.com> | 2012-03-04 00:34:37 (GMT) |
---|---|---|
committer | Victor Stinner <victor.stinner@gmail.com> | 2012-03-04 00:34:37 (GMT) |
commit | c9590ad745caa9fc76a8373d19e8019d90e8f9e8 (patch) | |
tree | f4ef97eba8cadeb127abe6f63d6ec0bf5e3b3196 /Objects | |
parent | d263d185534a8c19b719f27f5dab757afbc9319e (diff) | |
download | cpython-c9590ad745caa9fc76a8373d19e8019d90e8f9e8.zip cpython-c9590ad745caa9fc76a8373d19e8019d90e8f9e8.tar.gz cpython-c9590ad745caa9fc76a8373d19e8019d90e8f9e8.tar.bz2 |
Close #14085: remove assertions from PyUnicode_WRITE macro
Add checks in PyUnicode_WriteChar() and convert PyUnicode_New() assertion to a
test raising a Python exception.
Diffstat (limited to 'Objects')
-rw-r--r-- | Objects/unicodeobject.c | 11 |
1 files changed, 10 insertions, 1 deletions
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index a4dcdf6..b756afc 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -998,7 +998,11 @@ PyUnicode_New(Py_ssize_t size, Py_UCS4 maxchar) is_sharing = 1; } else { - assert(maxchar <= MAX_UNICODE); + if (maxchar > MAX_UNICODE) { + PyErr_SetString(PyExc_SystemError, + "invalid maximum character passed to PyUnicode_New"); + return NULL; + } kind_state = PyUnicode_4BYTE_KIND; char_size = 4; if (sizeof(wchar_t) == 4) @@ -3931,6 +3935,7 @@ PyUnicode_ReadChar(PyObject *unicode, Py_ssize_t index) int PyUnicode_WriteChar(PyObject *unicode, Py_ssize_t index, Py_UCS4 ch) { + Py_UCS4 maxchar; if (!PyUnicode_Check(unicode) || !PyUnicode_IS_COMPACT(unicode)) { PyErr_BadArgument(); return -1; @@ -3942,6 +3947,10 @@ PyUnicode_WriteChar(PyObject *unicode, Py_ssize_t index, Py_UCS4 ch) } if (unicode_check_modifiable(unicode)) return -1; + if (ch > PyUnicode_MAX_CHAR_VALUE(unicode)) { + PyErr_SetString(PyExc_ValueError, "character out of range"); + return -1; + } PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode), index, ch); return 0; |