gh-130317: Fix strict aliasing in PyFloat_Pack8() (#133150)

* Fix strict aliasing in PyFloat_Pack8() and PyFloat_Pack4().
* Fix _testcapi.float_set_snan() on x86 (32-bit).
This commit is contained in:
Victor Stinner 2025-04-29 16:27:50 +02:00 committed by GitHub
parent 698c6e3a0c
commit 02cd6d7097
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 12 additions and 9 deletions

View file

@ -182,8 +182,11 @@ _testcapi_float_set_snan(PyObject *module, PyObject *obj)
uint64_t v;
memcpy(&v, &d, 8);
v &= ~(1ULL << 51); /* make sNaN */
memcpy(&d, &v, 8);
return PyFloat_FromDouble(d);
// gh-130317: memcpy() is needed to preserve the sNaN flag on x86 (32-bit)
PyObject *res = PyFloat_FromDouble(0.0);
memcpy(&((PyFloatObject *)res)->ob_fval, &v, 8);
return res;
}
static PyMethodDef test_methods[] = {