From 5ecaf2541dd57c871f99b02e70334986a78481c2 Mon Sep 17 00:00:00 2001 From: Zackery Spytz Date: Sun, 12 Aug 2018 00:26:53 -0600 Subject: [PATCH 1/2] bpo-32745: Fix a regression in the handling of ctypes' c_wchar_p type Embedded nulls would cause a ValueError to be raised. Thanks go to Eryk Sun for their analysis. --- Lib/ctypes/test/test_unicode.py | 8 ++++++++ .../next/Library/2018-08-09-23-47-10.bpo-32745.iQi9hI.rst | 3 +++ Modules/_ctypes/cfield.c | 3 ++- 3 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2018-08-09-23-47-10.bpo-32745.iQi9hI.rst diff --git a/Lib/ctypes/test/test_unicode.py b/Lib/ctypes/test/test_unicode.py index c200af7b650661b..56a27fd5fd84e25 100644 --- a/Lib/ctypes/test/test_unicode.py +++ b/Lib/ctypes/test/test_unicode.py @@ -26,6 +26,14 @@ def test_buffers(self): 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 index 000000000000000..e6a60fe8da140b5 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2018-08-09-23-47-10.bpo-32745.iQi9hI.rst @@ -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. diff --git a/Modules/_ctypes/cfield.c b/Modules/_ctypes/cfield.c index e2b9aa8ed153c50..b0dc111e16f72f4 100644 --- a/Modules/_ctypes/cfield.c +++ b/Modules/_ctypes/cfield.c @@ -1355,6 +1355,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; @@ -1378,7 +1379,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); From b5c285d5c3f51dfd4918c22cdf36fa2949650192 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Langa?= Date: Sun, 2 May 2021 12:18:59 +0200 Subject: [PATCH 2/2] Bump CI --- Lib/ctypes/test/test_unicode.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/ctypes/test/test_unicode.py b/Lib/ctypes/test/test_unicode.py index 56a27fd5fd84e25..60c75424b767fad 100644 --- a/Lib/ctypes/test/test_unicode.py +++ b/Lib/ctypes/test/test_unicode.py @@ -30,7 +30,7 @@ def test_embedded_null(self): class TestStruct(ctypes.Structure): _fields_ = [("unicode", ctypes.c_wchar_p)] t = TestStruct() - # This would raise a ValueError. + # This would raise a ValueError: t.unicode = "foo\0bar\0\0"