]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-39593: Add test on ctypes cfield.c s_set() (GH-18424)
authorHai Shi <shihai1992@gmail.com>
Mon, 1 Jun 2020 16:54:18 +0000 (00:54 +0800)
committerGitHub <noreply@github.com>
Mon, 1 Jun 2020 16:54:18 +0000 (18:54 +0200)
Lib/ctypes/test/test_struct_fields.py
Modules/_ctypes/cfield.c

index 8045cc82679cc1e10c2a4a0723f6da64ea9abd7d..ee8415f3e630c29d08f6188a5b9e2085011f3b18 100644 (file)
@@ -46,6 +46,14 @@ class StructFieldsTestCase(unittest.TestCase):
         Y._fields_ = []
         self.assertRaises(AttributeError, setattr, X, "_fields_", [])
 
+    def test_5(self):
+        class X(Structure):
+            _fields_ = (("char", c_char * 5),)
+
+        x = X(b'#' * 5)
+        x.char = b'a\0b\0'
+        self.assertEqual(bytes(x), b'a\x00###')
+
     # __set__ and __get__ should raise a TypeError in case their self
     # argument is not a ctype instance.
     def test___set__(self):
index 7f853190a785e66d77f59e681218ee32b6c9f598..32a2beeb744f7cad829d3332f1c0a90c352262f7 100644 (file)
@@ -1263,7 +1263,9 @@ s_set(void *ptr, PyObject *value, Py_ssize_t length)
     }
 
     data = PyBytes_AS_STRING(value);
-    size = strlen(data); /* XXX Why not Py_SIZE(value)? */
+    // bpo-39593: Use strlen() to truncate the string at the first null character.
+    size = strlen(data);
+
     if (size < length) {
         /* This will copy the terminating NUL character
          * if there is space for it.