Skip to content

Commit 73766b0

Browse files
ZackerySpytzambv
andauthored
bpo-32745: Fix a regression in the handling of ctypes' c_wchar_p type (#8721)
Embedded nulls would cause a ValueError to be raised. Thanks go to Eryk Sun for their analysis. Co-authored-by: Łukasz Langa <[email protected]>
1 parent 518f8b5 commit 73766b0

File tree

3 files changed

+13
-1
lines changed

3 files changed

+13
-1
lines changed

Lib/ctypes/test/test_unicode.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ def test_buffers(self):
2626
self.assertEqual(buf[::2], 'a\xe4\xfc')
2727
self.assertEqual(buf[6:5:-1], "")
2828

29+
def test_embedded_null(self):
30+
class TestStruct(ctypes.Structure):
31+
_fields_ = [("unicode", ctypes.c_wchar_p)]
32+
t = TestStruct()
33+
# This would raise a ValueError:
34+
t.unicode = "foo\0bar\0\0"
35+
36+
2937
func = ctypes.CDLL(_ctypes_test.__file__)._testfunc_p_p
3038

3139
class StringTestCase(UnicodeTestCase):
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix a regression in the handling of ctypes' :data:`ctypes.c_wchar_p` type:
2+
embedded null characters would cause a :exc:`ValueError` to be raised. Patch
3+
by Zackery Spytz.

Modules/_ctypes/cfield.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1341,6 +1341,7 @@ Z_set(void *ptr, PyObject *value, Py_ssize_t size)
13411341
{
13421342
PyObject *keep;
13431343
wchar_t *buffer;
1344+
Py_ssize_t bsize;
13441345

13451346
if (value == Py_None) {
13461347
*(wchar_t **)ptr = NULL;
@@ -1364,7 +1365,7 @@ Z_set(void *ptr, PyObject *value, Py_ssize_t size)
13641365

13651366
/* We must create a wchar_t* buffer from the unicode object,
13661367
and keep it alive */
1367-
buffer = PyUnicode_AsWideCharString(value, NULL);
1368+
buffer = PyUnicode_AsWideCharString(value, &bsize);
13681369
if (!buffer)
13691370
return NULL;
13701371
keep = PyCapsule_New(buffer, CTYPES_CFIELD_CAPSULE_NAME_PYMEM, pymem_destructor);

0 commit comments

Comments
 (0)