8000 bpo-35714: Reject null characters in struct format strings (GH-16928) · python/cpython@3f59b55 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3f59b55

Browse files
authored
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.
1 parent 372ee27 commit 3f59b55

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

Lib/test/test_struct.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -671,6 +671,14 @@ def __del__(self):
671671
self.assertIn(b"Exception ignored in:", stderr)
672672
self.assertIn(b"C.__del__", stderr)
673673

674+
def test_issue35714(self):
675+
# Embedded null characters should not be allowed in format strings.
676+
for s in '\0', '2\0i', b'\0':
677+
with self.assertRaisesRegex(struct.error,
678+
'embedded null character'):
679+
struct.calcsize(s)
680+
681+
674682
class UnpackIteratorTest(unittest.TestCase):
675683
"""
676684
Tests for iterative unpacking (struct.Struct.iter_unpack).
Lines changed: 2 addition 8000 s & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
:exc:`struct.error` is now raised if there is a null character in a
2+
:mod:`struct` format string.

Modules/_struct.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1296,6 +1296,11 @@ prepare_s(PyStructObject *self)
12961296
size_t ncodes;
12971297

12981298
fmt = PyBytes_AS_STRING(self->s_format);
1299+
if (strlen(fmt) != (size_t)PyBytes_GET_SIZE(self->s_format)) {
1300+
PyErr_SetString(_structmodulestate_global->StructError,
1301+
"embedded null character");
1302+
return -1;
1303+
}
12991304

13001305
f = whichtable(&fmt);
13011306

0 commit comments

Comments
 (0)
0