| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -424,6 +424,20 @@ def test_705836(self): | |||
| 424 | 424 | self.assertRaises(OverflowError, struct.pack, "<e", big) | |
| 425 | 425 | self.assertRaises(OverflowError, struct.pack, "e", big) | |
| 426 | 426 | ||
| 427 | + def test_float_complex_overflow(self): | ||
| 428 | + for value in ( | ||
| 429 | + 1e300 + 0.5j, # big real | ||
| 430 | + 1.5 + 1e300j, # big imag | ||
| 431 | + ): | ||
| 432 | + for format in (">Zf", "<Zf", "Zf"): | ||
| 433 | + with self.subTest(value=value, format=format): | ||
| 434 | + self.assertRaises(OverflowError, struct.pack, format, value) | ||
| 435 | + | ||
| 436 | + ba = bytearray(8) | ||
| 437 | + with self.assertRaises(OverflowError): | ||
| 438 | + struct.Struct(format).pack_into(ba, 0, value) | ||
| 439 | + self.assertEqual(ba, bytearray(8)) | ||
| 440 | + | ||
| 427 | 441 | def test_1530559(self): | |
| 428 | 442 | for code, byteorder in iter_integer_formats(): | |
| 429 | 443 | format = byteorder + code | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,3 @@ | |||
| 1 | + Raise :exc:`OverflowError`'s for native ``'Zf'`` format in :func:`struct.pack`, | ||
| 2 | + like for ``'f'`` format. Previously overflows in the :c:expr:`float complex` | ||
| 3 | + type were silent. Patch by Sergey B Kirpichev. | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -790,14 +790,19 @@ np_float_complex(_structmodulestate *state, char *p, PyObject *v, | |||
| 790 | 790 | const formatdef *f) | |
| 791 | 791 | { | |
| 792 | 792 | Py_complex c = PyComplex_AsCComplex(v); | |
| 793 | - float x[2] = {(float)c.real, (float)c.imag}; | ||
| 793 | + char tmp[8]; | ||
| 794 | 794 | ||
| 795 | 795 | if (c.real == -1 && PyErr_Occurred()) { | |
| 796 | 796 | PyErr_SetString(state->StructError, | |
| 797 | 797 | "required argument is not a complex"); | |
| 798 | 798 | return -1; | |
| 799 | 799 | } | |
| 800 | - memcpy(p, &x, sizeof(x)); | ||
| 800 | + if (PyFloat_Pack4(c.real, tmp, PY_LITTLE_ENDIAN) | ||
| 801 | + || PyFloat_Pack4(c.imag, tmp + 4, PY_LITTLE_ENDIAN)) | ||
| 802 | + { | ||
| 803 | + return -1; | ||
| 804 | + } | ||
| 805 | + memcpy(p, tmp, 8); | ||
| 801 | 806 | return 0; | |
| 802 | 807 | } | |
| 803 | 808 | ||
@@ -1132,15 +1137,20 @@ static int | |||
| 1132 | 1137 | bp_float_complex(_structmodulestate *state, char *p, PyObject *v, const formatdef *f) | |
| 1133 | 1138 | { | |
| 1134 | 1139 | Py_complex x = PyComplex_AsCComplex(v); | |
| 1140 | + char tmp[8]; | ||
| 1141 | + | ||
| 1135 | 1142 | if (x.real == -1 && PyErr_Occurred()) { | |
| 1136 | 1143 | PyErr_SetString(state->StructError, | |
| 1137 | 1144 | "required argument is not a complex"); | |
| 1138 | 1145 | return -1; | |
| 1139 | 1146 | } | |
| 1140 | - if (PyFloat_Pack4(x.real, p, 0)) { | ||
| 1147 | + if (PyFloat_Pack4(x.real, tmp, 0) | ||
| 1148 | + || PyFloat_Pack4(x.imag, tmp + 4, 0)) | ||
| 1149 | + { | ||
| 1141 | 1150 | return -1; | |
| 1142 | 1151 | } | |
| 1143 | - return PyFloat_Pack4(x.imag, p + 4, 0); | ||
| 1152 | + memcpy(p, tmp, 8); | ||
| 1153 | + return 0; | ||
| 1144 | 1154 | } | |
| 1145 | 1155 | ||
| 1146 | 1156 | static int | |
@@ -1458,16 +1468,20 @@ static int | |||
| 1458 | 1468 | lp_float_complex(_structmodulestate *state, char *p, PyObject *v, const formatdef *f) | |
| 1459 | 1469 | { | |
| 1460 | 1470 | Py_complex x = PyComplex_AsCComplex(v); | |
| 1471 | + char tmp[8]; | ||
| 1472 | + | ||
| 1461 | 1473 | if (x.real == -1 && PyErr_Occurred()) { | |
| 1462 | 1474 | PyErr_SetString(state->StructError, | |
| 1463 | 1475 | "required argument is not a complex"); | |
| 1464 | 1476 | return -1; | |
| 1465 | 1477 | } | |
| 1466 | - if (PyFloat_Pack4(x.real, p, 1)) { | ||
| 1478 | + if (PyFloat_Pack4(x.real, tmp, 1) | ||
| 1479 | + || PyFloat_Pack4(x.imag, tmp + 4, 1)) | ||
| 1480 | + { | ||
| 1467 | 1481 | return -1; | |
| 1468 | 1482 | } | |
| 1469 | - return PyFloat_Pack4(x.imag, p + 4, 1); | ||
| 1470 | - | ||
| 1483 | + memcpy(p, tmp, 8); | ||
| 1484 | + return 0; | ||
| 1471 | 1485 | } | |
| 1472 | 1486 | ||
| 1473 | 1487 | static int | |
| Back | FazBrowse Home | New Git URL |
0 commit comments