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

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