]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.8] bpo-35714: Reject null characters in struct format strings (GH-16928) (GH-20419)
authorZackery Spytz <zspytz@gmail.com>
Tue, 26 May 2020 08:57:09 +0000 (02:57 -0600)
committerGitHub <noreply@github.com>
Tue, 26 May 2020 08:57:09 +0000 (11:57 +0300)
struct.error is now raised if there is a null character in a struct
format string.
(cherry picked from commit 3f59b55316f4c6ab451997902579aa69020b537c)

Lib/test/test_struct.py
Misc/NEWS.d/next/Library/2019-10-25-23-45-49.bpo-35714.fw3xb7.rst [new file with mode: 0644]
Modules/_struct.c

index 454082e66d3f86bdfbd81a24610f11d9d8808756..67e7c559d9f1d25f00c91162e7a01d08ea5129ee 100644 (file)
@@ -652,6 +652,13 @@ class StructTest(unittest.TestCase):
         s2 = struct.Struct(s.format.encode())
         self.assertEqual(s2.format, s.format)
 
+    def test_issue35714(self):
+        # Embedded null characters should not be allowed in format strings.
+        for s in '\0', '2\0i', b'\0':
+            with self.assertRaisesRegex(struct.error,
+                                        'embedded null character'):
+                struct.calcsize(s)
+
 
 class UnpackIteratorTest(unittest.TestCase):
     """
diff --git a/Misc/NEWS.d/next/Library/2019-10-25-23-45-49.bpo-35714.fw3xb7.rst b/Misc/NEWS.d/next/Library/2019-10-25-23-45-49.bpo-35714.fw3xb7.rst
new file mode 100644 (file)
index 0000000..3910206
--- /dev/null
@@ -0,0 +1,2 @@
+:exc:`struct.error` is now raised if there is a null character in a
+:mod:`struct` format string.
index 1c917b7513f46724b523a3ddc47e99f0f0c8a89b..64a9827e83aaeed473493518ce0231de4ae0a176 100644 (file)
@@ -1285,6 +1285,10 @@ prepare_s(PyStructObject *self)
     size_t ncodes;
 
     fmt = PyBytes_AS_STRING(self->s_format);
+    if (strlen(fmt) != (size_t)PyBytes_GET_SIZE(self->s_format)) {
+        PyErr_SetString(StructError, "embedded null character");
+        return -1;
+    }
 
     f = whichtable(&fmt);