Sitelet https://github.com/python/cpython/pull/20375/commits/90b0d96fe1c9d5bf56e14c6cf195e80962172a03
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 3f59b55)

Co-authored-by: Zackery Spytz <zspytz@gmail.com>
  • Loading branch information
ZackerySpytz authored and miss-islington committed May 25, 2020
commit 90b0d96fe1c9d5bf56e14c6cf195e80962172a03
8 changes: 8 additions & 0 deletions Lib/test/test_struct.py
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,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).
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:exc:`struct.error` is now raised if there is a null character in a
:mod:`struct` format string.
5 changes: 5 additions & 0 deletions Modules/_struct.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down