Skip to content
Open
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
16 changes: 16 additions & 0 deletions Lib/test/test_struct.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,22 @@ def test_705836(self):
self.assertRaises(OverflowError, struct.pack, "<e", big)
self.assertRaises(OverflowError, struct.pack, "e", big)

def test_float_complex_overflow(self):
for value in (
1e300 + 0.5j, # big real
1.5 + 1e300j, # big imag
):
for format in (">Zf", "<Zf", "Zf"):
with self.subTest(value=value, format=format):
self.assertRaises(OverflowError, struct.pack, format, value)

ba = bytearray(8)
try:
struct.Struct(format).pack_into(ba, 0, value)
assert False
except OverflowError:
self.assertEqual(ba, bytearray(8))

def test_1530559(self):
for code, byteorder in iter_integer_formats():
format = byteorder + code
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Raise :exc:`OverflowError`'s for native ``'Zf'`` format in :func:`struct.pack`,
like for ``'f'`` format. Previously overflows in the :c:expr:`float complex`
type were silent. Patch by Sergey B Kirpichev.
28 changes: 21 additions & 7 deletions Modules/_struct.c
Original file line number Diff line number Diff line change
Expand Up @@ -790,14 +790,19 @@ np_float_complex(_structmodulestate *state, char *p, PyObject *v,
const formatdef *f)
{
Py_complex c = PyComplex_AsCComplex(v);
float x[2] = {(float)c.real, (float)c.imag};
char tmp[8];

if (c.real == -1 && PyErr_Occurred()) {
PyErr_SetString(state->StructError,
"required argument is not a complex");
return -1;
}
memcpy(p, &x, sizeof(x));
if (PyFloat_Pack4(c.real, tmp, PY_LITTLE_ENDIAN)
|| PyFloat_Pack4(c.imag, tmp + 4, PY_LITTLE_ENDIAN))
{
return -1;
}
memcpy(p, tmp, 8);
return 0;
}

Expand Down Expand Up @@ -1132,15 +1137,20 @@ static int
bp_float_complex(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
{
Py_complex x = PyComplex_AsCComplex(v);
char tmp[8];

if (x.real == -1 && PyErr_Occurred()) {
PyErr_SetString(state->StructError,
"required argument is not a complex");
return -1;
}
if (PyFloat_Pack4(x.real, p, 0)) {
if (PyFloat_Pack4(x.real, tmp, 0)
|| PyFloat_Pack4(x.imag, tmp + 4, 0))
{
return -1;
}
return PyFloat_Pack4(x.imag, p + 4, 0);
memcpy(p, tmp, 8);
return 0;
}

static int
Expand Down Expand Up @@ -1458,16 +1468,20 @@ static int
lp_float_complex(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
{
Py_complex x = PyComplex_AsCComplex(v);
char tmp[8];

if (x.real == -1 && PyErr_Occurred()) {
PyErr_SetString(state->StructError,
"required argument is not a complex");
return -1;
}
if (PyFloat_Pack4(x.real, p, 1)) {
if (PyFloat_Pack4(x.real, tmp, 1)
|| PyFloat_Pack4(x.imag, tmp + 4, 1))
{
return -1;
}
return PyFloat_Pack4(x.imag, p + 4, 1);

memcpy(p, tmp, 8);
return 0;
}

static int
Expand Down
Loading