Skip to content

Commit

Permalink
[3.6] bpo-22207: Add checks for possible integer overflows in unicode…
Browse files Browse the repository at this point in the history
…object.c. (GH-2623) (#2658)

Based on patch by Victor Stinner.
(cherry picked from commit 64e461b)
  • Loading branch information
serhiy-storchaka committed Jul 11, 2017
1 parent ecfe4f6 commit 82a9075
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions Objects/unicodeobject.c
Expand Up @@ -5513,13 +5513,12 @@ _PyUnicode_EncodeUTF32(PyObject *str,
/* four bytes are reserved for each surrogate */
if (moreunits > 1) {
Py_ssize_t outpos = out - (uint32_t*) PyBytes_AS_STRING(v);
Py_ssize_t morebytes = 4 * (moreunits - 1);
if (PyBytes_GET_SIZE(v) > PY_SSIZE_T_MAX - morebytes) {
if (moreunits >= (PY_SSIZE_T_MAX - PyBytes_GET_SIZE(v)) / 4) {
/* integer overflow */
PyErr_NoMemory();
goto error;
}
if (_PyBytes_Resize(&v, PyBytes_GET_SIZE(v) + morebytes) < 0)
if (_PyBytes_Resize(&v, PyBytes_GET_SIZE(v) + 4 * (moreunits - 1)) < 0)
goto error;
out = (uint32_t*) PyBytes_AS_STRING(v) + outpos;
}
Expand Down Expand Up @@ -5865,13 +5864,12 @@ _PyUnicode_EncodeUTF16(PyObject *str,
/* two bytes are reserved for each surrogate */
if (moreunits > 1) {
Py_ssize_t outpos = out - (unsigned short*) PyBytes_AS_STRING(v);
Py_ssize_t morebytes = 2 * (moreunits - 1);
if (PyBytes_GET_SIZE(v) > PY_SSIZE_T_MAX - morebytes) {
if (moreunits >= (PY_SSIZE_T_MAX - PyBytes_GET_SIZE(v)) / 2) {
/* integer overflow */
PyErr_NoMemory();
goto error;
}
if (_PyBytes_Resize(&v, PyBytes_GET_SIZE(v) + morebytes) < 0)
if (_PyBytes_Resize(&v, PyBytes_GET_SIZE(v) + 2 * (moreunits - 1)) < 0)
goto error;
out = (unsigned short*) PyBytes_AS_STRING(v) + outpos;
}
Expand Down Expand Up @@ -6551,6 +6549,10 @@ _PyUnicode_DecodeUnicodeInternal(const char *s,
1))
return NULL;

if (size < 0) {
PyErr_BadInternalCall();
return NULL;
}
if (size == 0)
_Py_RETURN_UNICODE_EMPTY();

Expand Down Expand Up @@ -7352,6 +7354,10 @@ decode_code_page_stateful(int code_page,
PyErr_SetString(PyExc_ValueError, "invalid code page number");
return NULL;
}
if (size < 0) {
PyErr_BadInternalCall();
return NULL;
}

if (consumed)
*consumed = 0;
Expand Down

0 comments on commit 82a9075

Please sign in to comment.