[3.8] bpo-35714: Reject null characters in struct format strings (GH-16928) (GH-20419)
struct.error is now raised if there is a null character in a struct
format string.
(cherry picked from commit 3f59b55316
)
This commit is contained in:
parent
c2a177adf3
commit
5ff5edfef6
|
@ -652,6 +652,13 @@ class StructTest(unittest.TestCase):
|
|||
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):
|
||||
"""
|
||||
|
|
|
@ -0,0 +1,2 @@
|
|||
:exc:`struct.error` is now raised if there is a null character in a
|
||||
:mod:`struct` format string.
|
|
@ -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);
|
||||
|
||||
|
|
Loading…
Reference in New Issue