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.
This commit is contained in:
parent
372ee27d49
commit
3f59b55316
|
@ -671,6 +671,14 @@ class StructTest(unittest.TestCase):
|
||||||
self.assertIn(b"Exception ignored in:", stderr)
|
self.assertIn(b"Exception ignored in:", stderr)
|
||||||
self.assertIn(b"C.__del__", stderr)
|
self.assertIn(b"C.__del__", stderr)
|
||||||
|
|
||||||
|
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):
|
class UnpackIteratorTest(unittest.TestCase):
|
||||||
"""
|
"""
|
||||||
Tests for iterative unpacking (struct.Struct.iter_unpack).
|
Tests for iterative unpacking (struct.Struct.iter_unpack).
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
:exc:`struct.error` is now raised if there is a null character in a
|
||||||
|
:mod:`struct` format string.
|
|
@ -1296,6 +1296,11 @@ prepare_s(PyStructObject *self)
|
||||||
size_t ncodes;
|
size_t ncodes;
|
||||||
|
|
||||||
fmt = PyBytes_AS_STRING(self->s_format);
|
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);
|
f = whichtable(&fmt);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue