]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-32745: Fix a regression in the handling of ctypes' c_wchar_p type (GH-8721) ...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Sun, 2 May 2021 11:03:23 +0000 (04:03 -0700)
committerGitHub <noreply@github.com>
Sun, 2 May 2021 11:03:23 +0000 (13:03 +0200)
Embedded nulls would cause a ValueError to be raised. Thanks go to Eryk Sun for their analysis.

Co-authored-by: Ɓukasz Langa <lukasz@langa.pl>
(cherry picked from commit 73766b0341674f3920f4ea86a6f8288b801960f9)

Co-authored-by: Zackery Spytz <zspytz@gmail.com>
Lib/ctypes/test/test_unicode.py
Misc/NEWS.d/next/Library/2018-08-09-23-47-10.bpo-32745.iQi9hI.rst [new file with mode: 0644]
Modules/_ctypes/cfield.c

index c200af7b650661b0582ed8756e54154763888335..60c75424b767fadc50ff0af5a1d19a453089e4b8 100644 (file)
@@ -26,6 +26,14 @@ class UnicodeTestCase(unittest.TestCase):
         self.assertEqual(buf[::2], 'a\xe4\xfc')
         self.assertEqual(buf[6:5:-1], "")
 
+    def test_embedded_null(self):
+        class TestStruct(ctypes.Structure):
+            _fields_ = [("unicode", ctypes.c_wchar_p)]
+        t = TestStruct()
+        # This would raise a ValueError:
+        t.unicode = "foo\0bar\0\0"
+
+
 func = ctypes.CDLL(_ctypes_test.__file__)._testfunc_p_p
 
 class StringTestCase(UnicodeTestCase):
diff --git a/Misc/NEWS.d/next/Library/2018-08-09-23-47-10.bpo-32745.iQi9hI.rst b/Misc/NEWS.d/next/Library/2018-08-09-23-47-10.bpo-32745.iQi9hI.rst
new file mode 100644 (file)
index 0000000..e6a60fe
--- /dev/null
@@ -0,0 +1,3 @@
+Fix a regression in the handling of ctypes' :data:`ctypes.c_wchar_p` type:
+embedded null characters would cause a :exc:`ValueError` to be raised. Patch
+by Zackery Spytz.
index 1a36356ec76a617742cf1b6ed1a0c68c07ab1a56..0b12a448693b53368621f75b26829cb5b4f82785 100644 (file)
@@ -1360,6 +1360,7 @@ Z_set(void *ptr, PyObject *value, Py_ssize_t size)
 {
     PyObject *keep;
     wchar_t *buffer;
+    Py_ssize_t bsize;
 
     if (value == Py_None) {
         *(wchar_t **)ptr = NULL;
@@ -1383,7 +1384,7 @@ Z_set(void *ptr, PyObject *value, Py_ssize_t size)
 
     /* We must create a wchar_t* buffer from the unicode object,
        and keep it alive */
-    buffer = PyUnicode_AsWideCharString(value, NULL);
+    buffer = PyUnicode_AsWideCharString(value, &bsize);
     if (!buffer)
         return NULL;
     keep = PyCapsule_New(buffer, CTYPES_CFIELD_CAPSULE_NAME_PYMEM, pymem_destructor);