diff --git a/Lib/test/test_struct.py b/Lib/test/test_struct.py index 3809f2d26438955..c7ec6405aa92e15 100644 --- a/Lib/test/test_struct.py +++ b/Lib/test/test_struct.py @@ -424,6 +424,22 @@ def test_705836(self): self.assertRaises(OverflowError, struct.pack, "Zf", "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; } @@ -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 @@ -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