Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[3.8] bpo-35714: Reject null characters in struct format strings (GH-16928) #20419

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 26, 2020
Merged
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
7 changes: 7 additions & 0 deletions Lib/test/test_struct.py
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,13 @@ 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':
with self.assertRaisesRegex(struct.error,
'embedded null character'):
struct.calcsize(s)


class UnpackIteratorTest(unittest.TestCase):
"""
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.
4 changes: 4 additions & 0 deletions Modules/_struct.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down