Blame SOURCES/00357-bpo-42938-replace-snprintf-with-python-unicode-formatting-in-ctypes-param-reprs-gh-24247.patch

88b00c
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
88b00c
From: "Miss Islington (bot)"
88b00c
 <31488909+miss-islington@users.noreply.github.com>
88b00c
Date: Mon, 18 Jan 2021 13:29:31 -0800
88b00c
Subject: [PATCH] 00357: bpo-42938: Replace snprintf with Python unicode
88b00c
 formatting in ctypes param reprs. (GH-24247)
88b00c
88b00c
(cherry picked from commit 916610ef90a0d0761f08747f7b0905541f0977c7)
88b00c
88b00c
Co-authored-by: Benjamin Peterson <benjamin@python.org>
88b00c
---
88b00c
 Lib/ctypes/test/test_parameters.py            | 43 ++++++++++++++++
88b00c
 .../2021-01-18-09-27-31.bpo-42938.4Zn4Mp.rst  |  2 +
88b00c
 Modules/_ctypes/callproc.c                    | 51 +++++++------------
88b00c
 3 files changed, 64 insertions(+), 32 deletions(-)
88b00c
 create mode 100644 Misc/NEWS.d/next/Security/2021-01-18-09-27-31.bpo-42938.4Zn4Mp.rst
88b00c
88b00c
diff --git a/Lib/ctypes/test/test_parameters.py b/Lib/ctypes/test/test_parameters.py
88b00c
index e4c25fd880..531894fdec 100644
88b00c
--- a/Lib/ctypes/test/test_parameters.py
88b00c
+++ b/Lib/ctypes/test/test_parameters.py
88b00c
@@ -201,6 +201,49 @@ class SimpleTypesTestCase(unittest.TestCase):
88b00c
         with self.assertRaises(ZeroDivisionError):
88b00c
             WorseStruct().__setstate__({}, b'foo')
88b00c
 
88b00c
+    def test_parameter_repr(self):
88b00c
+        from ctypes import (
88b00c
+            c_bool,
88b00c
+            c_char,
88b00c
+            c_wchar,
88b00c
+            c_byte,
88b00c
+            c_ubyte,
88b00c
+            c_short,
88b00c
+            c_ushort,
88b00c
+            c_int,
88b00c
+            c_uint,
88b00c
+            c_long,
88b00c
+            c_ulong,
88b00c
+            c_longlong,
88b00c
+            c_ulonglong,
88b00c
+            c_float,
88b00c
+            c_double,
88b00c
+            c_longdouble,
88b00c
+            c_char_p,
88b00c
+            c_wchar_p,
88b00c
+            c_void_p,
88b00c
+        )
88b00c
+        self.assertRegex(repr(c_bool.from_param(True)), r"^<cparam '\?' at 0x[A-Fa-f0-9]+>$")
88b00c
+        self.assertEqual(repr(c_char.from_param(97)), "<cparam 'c' ('a')>")
88b00c
+        self.assertRegex(repr(c_wchar.from_param('a')), r"^<cparam 'u' at 0x[A-Fa-f0-9]+>$")
88b00c
+        self.assertEqual(repr(c_byte.from_param(98)), "<cparam 'b' (98)>")
88b00c
+        self.assertEqual(repr(c_ubyte.from_param(98)), "<cparam 'B' (98)>")
88b00c
+        self.assertEqual(repr(c_short.from_param(511)), "<cparam 'h' (511)>")
88b00c
+        self.assertEqual(repr(c_ushort.from_param(511)), "<cparam 'H' (511)>")
88b00c
+        self.assertRegex(repr(c_int.from_param(20000)), r"^<cparam '[li]' \(20000\)>$")
88b00c
+        self.assertRegex(repr(c_uint.from_param(20000)), r"^<cparam '[LI]' \(20000\)>$")
88b00c
+        self.assertRegex(repr(c_long.from_param(20000)), r"^<cparam '[li]' \(20000\)>$")
88b00c
+        self.assertRegex(repr(c_ulong.from_param(20000)), r"^<cparam '[LI]' \(20000\)>$")
88b00c
+        self.assertRegex(repr(c_longlong.from_param(20000)), r"^<cparam '[liq]' \(20000\)>$")
88b00c
+        self.assertRegex(repr(c_ulonglong.from_param(20000)), r"^<cparam '[LIQ]' \(20000\)>$")
88b00c
+        self.assertEqual(repr(c_float.from_param(1.5)), "<cparam 'f' (1.5)>")
88b00c
+        self.assertEqual(repr(c_double.from_param(1.5)), "<cparam 'd' (1.5)>")
88b00c
+        self.assertEqual(repr(c_double.from_param(1e300)), "<cparam 'd' (1e+300)>")
88b00c
+        self.assertRegex(repr(c_longdouble.from_param(1.5)), r"^<cparam ('d' \(1.5\)|'g' at 0x[A-Fa-f0-9]+)>$")
88b00c
+        self.assertRegex(repr(c_char_p.from_param(b'hihi')), "^<cparam 'z' \(0x[A-Fa-f0-9]+\)>$")
88b00c
+        self.assertRegex(repr(c_wchar_p.from_param('hihi')), "^<cparam 'Z' \(0x[A-Fa-f0-9]+\)>$")
88b00c
+        self.assertRegex(repr(c_void_p.from_param(0x12)), r"^<cparam 'P' \(0x0*12\)>$")
88b00c
+
88b00c
 ################################################################
88b00c
 
88b00c
 if __name__ == '__main__':
88b00c
diff --git a/Misc/NEWS.d/next/Security/2021-01-18-09-27-31.bpo-42938.4Zn4Mp.rst b/Misc/NEWS.d/next/Security/2021-01-18-09-27-31.bpo-42938.4Zn4Mp.rst
88b00c
new file mode 100644
88b00c
index 0000000000..7df65a156f
88b00c
--- /dev/null
88b00c
+++ b/Misc/NEWS.d/next/Security/2021-01-18-09-27-31.bpo-42938.4Zn4Mp.rst
88b00c
@@ -0,0 +1,2 @@
88b00c
+Avoid static buffers when computing the repr of :class:`ctypes.c_double` and
88b00c
+:class:`ctypes.c_longdouble` values.
88b00c
diff --git a/Modules/_ctypes/callproc.c b/Modules/_ctypes/callproc.c
88b00c
index b0a36a3024..f2506de544 100644
88b00c
--- a/Modules/_ctypes/callproc.c
88b00c
+++ b/Modules/_ctypes/callproc.c
88b00c
@@ -489,58 +489,47 @@ is_literal_char(unsigned char c)
88b00c
 static PyObject *
88b00c
 PyCArg_repr(PyCArgObject *self)
88b00c
 {
88b00c
-    char buffer[256];
88b00c
     switch(self->tag) {
88b00c
     case 'b':
88b00c
     case 'B':
88b00c
-        sprintf(buffer, "<cparam '%c' (%d)>",
88b00c
+        return PyUnicode_FromFormat("<cparam '%c' (%d)>",
88b00c
             self->tag, self->value.b);
88b00c
-        break;
88b00c
     case 'h':
88b00c
     case 'H':
88b00c
-        sprintf(buffer, "<cparam '%c' (%d)>",
88b00c
+        return PyUnicode_FromFormat("<cparam '%c' (%d)>",
88b00c
             self->tag, self->value.h);
88b00c
-        break;
88b00c
     case 'i':
88b00c
     case 'I':
88b00c
-        sprintf(buffer, "<cparam '%c' (%d)>",
88b00c
+        return PyUnicode_FromFormat("<cparam '%c' (%d)>",
88b00c
             self->tag, self->value.i);
88b00c
-        break;
88b00c
     case 'l':
88b00c
     case 'L':
88b00c
-        sprintf(buffer, "<cparam '%c' (%ld)>",
88b00c
+        return PyUnicode_FromFormat("<cparam '%c' (%ld)>",
88b00c
             self->tag, self->value.l);
88b00c
-        break;
88b00c
 
88b00c
     case 'q':
88b00c
     case 'Q':
88b00c
-        sprintf(buffer,
88b00c
-#ifdef MS_WIN32
88b00c
-            "<cparam '%c' (%I64d)>",
88b00c
-#else
88b00c
-            "<cparam '%c' (%lld)>",
88b00c
-#endif
88b00c
+        return PyUnicode_FromFormat("<cparam '%c' (%lld)>",
88b00c
             self->tag, self->value.q);
88b00c
-        break;
88b00c
     case 'd':
88b00c
-        sprintf(buffer, "<cparam '%c' (%f)>",
88b00c
-            self->tag, self->value.d);
88b00c
-        break;
88b00c
-    case 'f':
88b00c
-        sprintf(buffer, "<cparam '%c' (%f)>",
88b00c
-            self->tag, self->value.f);
88b00c
-        break;
88b00c
-
88b00c
+    case 'f': {
88b00c
+        PyObject *f = PyFloat_FromDouble((self->tag == 'f') ? self->value.f : self->value.d);
88b00c
+        if (f == NULL) {
88b00c
+            return NULL;
88b00c
+        }
88b00c
+        PyObject *result = PyUnicode_FromFormat("<cparam '%c' (%R)>", self->tag, f);
88b00c
+        Py_DECREF(f);
88b00c
+        return result;
88b00c
+    }
88b00c
     case 'c':
88b00c
         if (is_literal_char((unsigned char)self->value.c)) {
88b00c
-            sprintf(buffer, "<cparam '%c' ('%c')>",
88b00c
+            return PyUnicode_FromFormat("<cparam '%c' ('%c')>",
88b00c
                 self->tag, self->value.c);
88b00c
         }
88b00c
         else {
88b00c
-            sprintf(buffer, "<cparam '%c' ('\\x%02x')>",
88b00c
+            return PyUnicode_FromFormat("<cparam '%c' ('\\x%02x')>",
88b00c
                 self->tag, (unsigned char)self->value.c);
88b00c
         }
88b00c
-        break;
88b00c
 
88b00c
 /* Hm, are these 'z' and 'Z' codes useful at all?
88b00c
    Shouldn't they be replaced by the functionality of c_string
88b00c
@@ -549,22 +538,20 @@ PyCArg_repr(PyCArgObject *self)
88b00c
     case 'z':
88b00c
     case 'Z':
88b00c
     case 'P':
88b00c
-        sprintf(buffer, "<cparam '%c' (%p)>",
88b00c
+        return PyUnicode_FromFormat("<cparam '%c' (%p)>",
88b00c
             self->tag, self->value.p);
88b00c
         break;
88b00c
 
88b00c
     default:
88b00c
         if (is_literal_char((unsigned char)self->tag)) {
88b00c
-            sprintf(buffer, "<cparam '%c' at %p>",
88b00c
+            return PyUnicode_FromFormat("<cparam '%c' at %p>",
88b00c
                 (unsigned char)self->tag, (void *)self);
88b00c
         }
88b00c
         else {
88b00c
-            sprintf(buffer, "<cparam 0x%02x at %p>",
88b00c
+            return PyUnicode_FromFormat("<cparam 0x%02x at %p>",
88b00c
                 (unsigned char)self->tag, (void *)self);
88b00c
         }
88b00c
-        break;
88b00c
     }
88b00c
-    return PyUnicode_FromString(buffer);
88b00c
 }
88b00c
 
88b00c
 static PyMemberDef PyCArgType_members[] = {