]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[2.7] bpo-38540: Fix possible leak in PyArg_Parse for "es#" and "et#". (GH-16869...
authorSerhiy Storchaka <storchaka@gmail.com>
Mon, 21 Oct 2019 18:40:30 +0000 (21:40 +0300)
committerGitHub <noreply@github.com>
Mon, 21 Oct 2019 18:40:30 +0000 (21:40 +0300)
(cherry picked from commit 5bc6a7c06eda20ba131ecba6752be0506d310181)

Misc/NEWS.d/next/C API/2019-10-21-09-24-03.bpo-38540.314N_T.rst [new file with mode: 0644]
Python/getargs.c

diff --git a/Misc/NEWS.d/next/C API/2019-10-21-09-24-03.bpo-38540.314N_T.rst b/Misc/NEWS.d/next/C API/2019-10-21-09-24-03.bpo-38540.314N_T.rst
new file mode 100644 (file)
index 0000000..1d73ad8
--- /dev/null
@@ -0,0 +1,3 @@
+Fixed possible leak in :c:func:`PyArg_Parse` and similar functions for
+format units ``"es#"`` and ``"et#"`` when the macro
+:c:macro:`PY_SSIZE_T_CLEAN` is not defined.
index cc1ddde977f555914b5a38b39ea8afb07d651a7b..12ee2486377f6db728113b2c1804a065cf5b03ec 100644 (file)
@@ -1156,7 +1156,19 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
             memcpy(*buffer,
                    PyString_AS_STRING(s),
                    size + 1);
-            STORE_SIZE(size);
+
+            if (flags & FLAG_SIZE_T) {
+                *q2 = size;
+            }
+            else {
+                if (INT_MAX < size) {
+                    Py_DECREF(s);
+                    PyErr_SetString(PyExc_OverflowError,
+                                    "size does not fit in an int");
+                    return converterr("", arg, msgbuf, bufsize);
+                }
+                *q = (int)size;
+            }
         } else {
             /* Using a 0-terminated buffer: