]> granicus.if.org Git - python/commitdiff
fix possible overflow in encode_basestring_ascii (#23369)
authorBenjamin Peterson <benjamin@python.org>
Sat, 13 Aug 2016 23:47:25 +0000 (16:47 -0700)
committerBenjamin Peterson <benjamin@python.org>
Sat, 13 Aug 2016 23:47:25 +0000 (16:47 -0700)
Misc/NEWS
Modules/_json.c

index 0aa7ee87c45ea02817b69ab6f5c0b64212682c22..9cb550ad0820a397fe0097b67fa93bdbbb13f477 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -29,6 +29,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #23369: Fixed possible integer overflow in
+  _json.encode_basestring_ascii.
+
 - Issue #27568: Prevent HTTPoxy attack (CVE-2016-1000110). Ignore the
   HTTP_PROXY variable when REQUEST_METHOD environment is set, which indicates
   that the script is in CGI mode.
index fede6b14a17cfaf1e4e75d13bcf5234947887da8..c3015460578e5b66c52ba1c5c1fc18cf3824e3dd 100644 (file)
@@ -211,6 +211,10 @@ ascii_escape_unicode(PyObject *pystr)
     input_unicode = PyUnicode_AS_UNICODE(pystr);
 
     /* One char input can be up to 6 chars output, estimate 4 of these */
+    if (input_chars > (PY_SSIZE_T_MAX - 2)/ MAX_EXPANSION) {
+        PyErr_SetString(PyExc_OverflowError, "string is too long to escape");
+        return NULL;
+    }
     output_size = 2 + (MIN_EXPANSION * 4) + input_chars;
     max_output_size = 2 + (input_chars * MAX_EXPANSION);
     rval = PyString_FromStringAndSize(NULL, output_size);