]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
prevent integer overflow in escape_unicode (closes #24522)
authorBenjamin Peterson <benjamin@python.org>
Sat, 27 Jun 2015 20:01:51 +0000 (15:01 -0500)
committerBenjamin Peterson <benjamin@python.org>
Sat, 27 Jun 2015 20:01:51 +0000 (15:01 -0500)
Misc/NEWS
Modules/_json.c

index 1f5241b8d0becfe63a4d85040540eab9b41241ff..4b3e189fc7bea1b35041d53d7fbe8c90fef157d8 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -24,6 +24,8 @@ Core and Builtins
 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
index e4478ef420c0ef61f20d95a431f1bfff69b39104..8000f915f3484b7c24654b38e709838bd6e58eef 100644 (file)
@@ -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;
+            = 2;
             break;
         default:
             if (c <= 0x1f)
-                output_size += 6;
+                = 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);