Skip to content

Commit a97011b

Browse files
authored
bpo-39593: Add test on ctypes cfield.c s_set() (GH-18424)
1 parent e9684fa commit a97011b

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

Lib/ctypes/test/test_struct_fields.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@ class Y(X):
4646
Y._fields_ = []
4747
self.assertRaises(AttributeError, setattr, X, "_fields_", [])
4848

49+
def test_5(self):
50+
class X(Structure):
51+
_fields_ = (("char", c_char * 5),)
52+
53+
x = X(b'#' * 5)
54+
x.char = b'a\0b\0'
55+
self.assertEqual(bytes(x), b'a\x00###')
56+
4957
# __set__ and __get__ should raise a TypeError in case their self
5058
# argument is not a ctype instance.
5159
def test___set__(self):

Modules/_ctypes/cfield.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1263,7 +1263,9 @@ s_set(void *ptr, PyObject *value, Py_ssize_t length)
12631263
}
12641264

12651265
data = PyBytes_AS_STRING(value);
1266-
size = strlen(data); /* XXX Why not Py_SIZE(value)? */
1266+
// bpo-39593: Use strlen() to truncate the string at the first null character.
1267+
size = strlen(data);
1268+
12671269
if (size < length) {
12681270
/* This will copy the terminating NUL character
12691271
* if there is space for it.

0 commit comments

Comments
 (0)