From 06175ec2fcfa29b5dd610e4630e7f8c21bdddaba Mon Sep 17 00:00:00 2001 From: Zackery Spytz Date: Mon, 25 May 2020 01:55:09 -0600 Subject: [PATCH 1/2] [3.8] 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 --- Lib/test/test_struct.py | 8 ++++++++ .../next/Library/2019-10-25-23-45-49.bpo-35714.fw3xb7.rst | 2 ++ Modules/_struct.c | 5 +++++ 3 files changed, 15 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2019-10-25-23-45-49.bpo-35714.fw3xb7.rst diff --git a/Lib/test/test_struct.py b/Lib/test/test_struct.py index 454082e66d3f86..4d328ce8fdf5f9 100644 --- a/Lib/test/test_struct.py +++ b/Lib/test/test_struct.py @@ -653,6 +653,14 @@ def test_format_attr(self): 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): """ 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 00000000000000..39102065ca7b51 --- /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 1c917b7513f467..6a42f39ab08360 100644 --- a/Modules/_struct.c +++ b/Modules/_struct.c @@ -1285,6 +1285,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); From f0c4e0f39195cc9bb06b582534f45176cd93eaed Mon Sep 17 00:00:00 2001 From: Zackery Spytz Date: Tue, 26 May 2020 02:17:55 -0600 Subject: [PATCH 2/2] Fixes for 3.8 --- Lib/test/test_struct.py | 1 - Modules/_struct.c | 3 +-- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/Lib/test/test_struct.py b/Lib/test/test_struct.py index 4d328ce8fdf5f9..67e7c559d9f1d2 100644 --- a/Lib/test/test_struct.py +++ b/Lib/test/test_struct.py @@ -652,7 +652,6 @@ def test_format_attr(self): 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': diff --git a/Modules/_struct.c b/Modules/_struct.c index 6a42f39ab08360..64a9827e83aaee 100644 --- a/Modules/_struct.c +++ b/Modules/_struct.c @@ -1286,8 +1286,7 @@ prepare_s(PyStructObject *self) 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"); + PyErr_SetString(StructError, "embedded null character"); return -1; }