diff options
author | Benjamin Peterson <benjamin@python.org> | 2015-06-27 20:01:51 (GMT) |
---|---|---|
committer | Benjamin Peterson <benjamin@python.org> | 2015-06-27 20:01:51 (GMT) |
commit | 7b78d4364da086baf77202e6e9f6839128a366ff (patch) | |
tree | 8d0fa068254a2d92fc9bfd5df7bfbceaa7be5e71 /Modules | |
parent | 758d60baaa3c041d0982c84d514719ab197bd6ed (diff) | |
download | cpython-7b78d4364da086baf77202e6e9f6839128a366ff.zip cpython-7b78d4364da086baf77202e6e9f6839128a366ff.tar.gz cpython-7b78d4364da086baf77202e6e9f6839128a366ff.tar.bz2 |
prevent integer overflow in escape_unicode (closes #24522)
Diffstat (limited to 'Modules')
-rw-r--r-- | Modules/_json.c | 12 |
1 files changed, 9 insertions, 3 deletions
diff --git a/Modules/_json.c b/Modules/_json.c index e4478ef..8000f91 100644 --- a/Modules/_json.c +++ b/Modules/_json.c @@ -249,17 +249,23 @@ escape_unicode(PyObject *pystr) /* 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); |