]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-35552: Fix reading past the end in PyUnicode_FromFormat() and PyBytes_FromFormat...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Sat, 12 Jan 2019 08:52:55 +0000 (00:52 -0800)
committerGitHub <noreply@github.com>
Sat, 12 Jan 2019 08:52:55 +0000 (00:52 -0800)
Format characters "%s" and "%V" in PyUnicode_FromFormat() and "%s" in PyBytes_FromFormat()
no longer read memory past the limit if precision is specified.
(cherry picked from commit d586ccb04f79863c819b212ec5b9d873964078e4)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Misc/NEWS.d/next/Core and Builtins/2018-12-21-13-29-30.bpo-35552.1DzQQc.rst [new file with mode: 0644]
Objects/bytesobject.c
Objects/unicodeobject.c

diff --git a/Misc/NEWS.d/next/Core and Builtins/2018-12-21-13-29-30.bpo-35552.1DzQQc.rst b/Misc/NEWS.d/next/Core and Builtins/2018-12-21-13-29-30.bpo-35552.1DzQQc.rst
new file mode 100644 (file)
index 0000000..dbc00bc
--- /dev/null
@@ -0,0 +1,3 @@
+Format characters ``%s`` and ``%V`` in :c:func:`PyUnicode_FromFormat` and
+``%s`` in :c:func:`PyBytes_FromFormat` no longer read memory past the
+limit if *precision* is specified.
index 5f9e1eccf2e4a0479f96821c369a400d0d1a01dc..172c7f38b9e258a6db2c90e578571e78f817399d 100644 (file)
@@ -311,9 +311,15 @@ PyBytes_FromFormatV(const char *format, va_list vargs)
             Py_ssize_t i;
 
             p = va_arg(vargs, const char*);
-            i = strlen(p);
-            if (prec > 0 && i > prec)
-                i = prec;
+            if (prec <= 0) {
+                i = strlen(p);
+            }
+            else {
+                i = 0;
+                while (i < prec && p[i]) {
+                    i++;
+                }
+            }
             s = _PyBytesWriter_WriteBytes(&writer, s, p, i);
             if (s == NULL)
                 goto error;
index 35c8a24b7c0cd800c36bfd48254fb41dfaf66912..b67ffac4e9fb98466246df28baef948c0abd8e66 100644 (file)
@@ -2579,9 +2579,15 @@ unicode_fromformat_write_cstr(_PyUnicodeWriter *writer, const char *str,
     PyObject *unicode;
     int res;
 
-    length = strlen(str);
-    if (precision != -1)
-        length = Py_MIN(length, precision);
+    if (precision == -1) {
+        length = strlen(str);
+    }
+    else {
+        length = 0;
+        while (length < precision && str[length]) {
+            length++;
+        }
+    }
     unicode = PyUnicode_DecodeUTF8Stateful(str, length, "replace", NULL);
     if (unicode == NULL)
         return -1;