churchyard / rpms / python3

Forked from rpms/python3 3 years ago
Clone

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

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