diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2020-05-26 07:04:59 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-05-26 07:04:59 (GMT) |
commit | 5221a10dde4a3853fe7ace316d95767648055109 (patch) | |
tree | 502c4fee95ca0c75364abd0ba40da7aa4571cb76 | |
parent | a729f4abb8a3b13b63d46c450faf64202751449b (diff) | |
download | cpython-5221a10dde4a3853fe7ace316d95767648055109.zip cpython-5221a10dde4a3853fe7ace316d95767648055109.tar.gz cpython-5221a10dde4a3853fe7ace316d95767648055109.tar.bz2 |
bpo-35714: Reject null characters in struct format strings (GH-16928)
struct.error is now raised if there is a null character in a struct
format string.
(cherry picked from commit 3f59b55316f4c6ab451997902579aa69020b537c)
Co-authored-by: Zackery Spytz <zspytz@gmail.com>
-rw-r--r-- | Lib/test/test_struct.py | 8 | ||||
-rw-r--r-- | Misc/NEWS.d/next/Library/2019-10-25-23-45-49.bpo-35714.fw3xb7.rst | 2 | ||||
-rw-r--r-- | Modules/_struct.c | 5 |
3 files changed, 15 insertions, 0 deletions
diff --git a/Lib/test/test_struct.py b/Lib/test/test_struct.py index 4829fbe..b3f21ea 100644 --- a/Lib/test/test_struct.py +++ b/Lib/test/test_struct.py @@ -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 index 0000000..3910206 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2019-10-25-23-45-49.bpo-35714.fw3xb7.rst @@ -0,0 +1,2 @@ +:exc:`struct.error` is now raised if there is a null character in a +:mod:`struct` format string. diff --git a/Modules/_struct.c b/Modules/_struct.c index 13d8072..5984bb6 100644 --- a/Modules/_struct.c +++ b/Modules/_struct.c @@ -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); |