changeset: 96693:0540e14c4b64 branch: 3.5 parent: 96690:bb8959d0540c user: Benjamin Peterson date: Sat Jun 27 15:01:51 2015 -0500 files: Misc/NEWS Modules/_json.c description: prevent integer overflow in escape_unicode (closes #24522) diff -r bb8959d0540c -r 0540e14c4b64 Misc/NEWS --- a/Misc/NEWS Sat Jun 27 14:26:21 2015 -0500 +++ b/Misc/NEWS Sat Jun 27 15:01:51 2015 -0500 @@ -24,6 +24,8 @@ Library ------- +- Issue #24522: Fix possible integer overflow in json accelerator module. + - Issue #24489: ensure a previously set C errno doesn't disturb cmath.polar(). - Issue #24408: Fixed AttributeError in measure() and metrics() methods of diff -r bb8959d0540c -r 0540e14c4b64 Modules/_json.c --- a/Modules/_json.c Sat Jun 27 14:26:21 2015 -0500 +++ b/Modules/_json.c Sat Jun 27 15:01:51 2015 -0500 @@ -249,17 +249,23 @@ /* Compute the output size */ for (i = 0, output_size = 2; i < input_chars; i++) { Py_UCS4 c = PyUnicode_READ(kind, input, i); + Py_ssize_t d; switch (c) { case '\\': case '"': case '\b': case '\f': case '\n': case '\r': case '\t': - output_size += 2; + d = 2; break; default: if (c <= 0x1f) - output_size += 6; + d = 6; else - output_size++; + d = 1; } + if (output_size > PY_SSIZE_T_MAX - d) { + PyErr_SetString(PyExc_OverflowError, "string is too long to escape"); + return NULL; + } + output_size += d; } rval = PyUnicode_New(output_size, maxchar);