diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2014-10-04 11:15:49 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2014-10-04 11:15:49 (GMT) |
commit | 2e374098ff791c81576ff2ba2961dc5011a693bf (patch) | |
tree | d959477354c67d9201b47629ce5cd8dd25f66120 /Python/codecs.c | |
parent | 518e71b18a008947b17369de5c06d9543db7dfc5 (diff) | |
download | cpython-2e374098ff791c81576ff2ba2961dc5011a693bf.zip cpython-2e374098ff791c81576ff2ba2961dc5011a693bf.tar.gz cpython-2e374098ff791c81576ff2ba2961dc5011a693bf.tar.bz2 |
Issue #22518: Fixed integer overflow issues in "backslashreplace",
"xmlcharrefreplace", and "surrogatepass" error handlers.
Diffstat (limited to 'Python/codecs.c')
-rw-r--r-- | Python/codecs.c | 10 |
1 files changed, 8 insertions, 2 deletions
diff --git a/Python/codecs.c b/Python/codecs.c index e584acc..6849c0f 100644 --- a/Python/codecs.c +++ b/Python/codecs.c @@ -773,7 +773,7 @@ PyObject *PyCodec_XMLCharRefReplaceErrors(PyObject *exc) Py_ssize_t end; PyObject *res; unsigned char *outp; - int ressize; + Py_ssize_t ressize; Py_UCS4 ch; if (PyUnicodeEncodeError_GetStart(exc, &start)) return NULL; @@ -781,6 +781,8 @@ PyObject *PyCodec_XMLCharRefReplaceErrors(PyObject *exc) return NULL; if (!(object = PyUnicodeEncodeError_GetObject(exc))) return NULL; + if (end - start > PY_SSIZE_T_MAX / (2+7+1)) + end = start + PY_SSIZE_T_MAX / (2+7+1); for (i = start, ressize = 0; i < end; ++i) { /* object is guaranteed to be "ready" */ ch = PyUnicode_READ_CHAR(object, i); @@ -869,7 +871,7 @@ PyObject *PyCodec_BackslashReplaceErrors(PyObject *exc) Py_ssize_t end; PyObject *res; unsigned char *outp; - int ressize; + Py_ssize_t ressize; Py_UCS4 c; if (PyUnicodeEncodeError_GetStart(exc, &start)) return NULL; @@ -877,6 +879,8 @@ PyObject *PyCodec_BackslashReplaceErrors(PyObject *exc) return NULL; if (!(object = PyUnicodeEncodeError_GetObject(exc))) return NULL; + if (end - start > PY_SSIZE_T_MAX / (1+1+8)) + end = start + PY_SSIZE_T_MAX / (1+1+8); for (i = start, ressize = 0; i < end; ++i) { /* object is guaranteed to be "ready" */ c = PyUnicode_READ_CHAR(object, i); @@ -1023,6 +1027,8 @@ PyCodec_SurrogatePassErrors(PyObject *exc) code = get_standard_encoding(encoding, &bytelength); Py_DECREF(encode); + if (end - start > PY_SSIZE_T_MAX / bytelength) + end = start + PY_SSIZE_T_MAX / bytelength; res = PyBytes_FromStringAndSize(NULL, bytelength*(end-start)); if (!res) { Py_DECREF(object); |