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