]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-35714: Reject null characters in struct format strings (GH-16928)
authorZackery Spytz <zspytz@gmail.com>
Mon, 25 May 2020 07:55:09 +0000 (01:55 -0600)
committerGitHub <noreply@github.com>
Mon, 25 May 2020 07:55:09 +0000 (10:55 +0300)
struct.error is now raised if there is a null character in a struct
format string.

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 4829fbe1b975cfe26df2fa352f856880bf55ec07..b3f21ea7db49ef3d2aed55489cba5c985e3a2db9 100644 (file)
@@ -671,6 +671,14 @@ class StructTest(unittest.TestCase):
         self.assertIn(b"Exception ignored in:", stderr)
         self.assertIn(b"C.__del__", stderr)
 
+    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):
     """
     Tests for iterative unpacking (struct.Struct.iter_unpack).
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 13d8072f61218753909995948dc6aa92970cc00c..5984bb68114368af32814e481d502c429381757f 100644 (file)
@@ -1296,6 +1296,11 @@ 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(_structmodulestate_global->StructError,
+                        "embedded null character");
+        return -1;
+    }
 
     f = whichtable(&fmt);