]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
refactor(c): more idiomatic type-punned pointer warning workaround 877/head
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Wed, 24 Jul 2024 08:37:30 +0000 (10:37 +0200)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Wed, 24 Jul 2024 09:54:04 +0000 (11:54 +0200)
We are using memcpy in all the other cases.

psycopg_c/psycopg_c/types/numeric.pyx

index ff4dacec2aa5a55dea91d0194f87eba4b4b4b0b5..31dc75b3e896db1d85b39360b8cdcab68b52b44a 100644 (file)
@@ -393,10 +393,9 @@ cdef class Float4BinaryLoader(CLoader):
         cdef uint32_t bedata
         memcpy(&bedata, data, sizeof(bedata))
         cdef uint32_t asint = endian.be32toh(bedata)
-        # avoid warning:
-        # dereferencing type-punned pointer will break strict-aliasing rules
-        cdef char *swp = <char *>&asint
-        return PyFloat_FromDouble((<float *>swp)[0])
+        cdef float f
+        memcpy(&f, &asint, sizeof(asint))
+        return PyFloat_FromDouble(f)
 
 
 @cython.final
@@ -408,8 +407,9 @@ cdef class Float8BinaryLoader(CLoader):
         cdef uint64_t bedata
         memcpy(&bedata, data, sizeof(bedata))
         cdef uint64_t asint = endian.be64toh(bedata)
-        cdef char *swp = <char *>&asint
-        return PyFloat_FromDouble((<double *>swp)[0])
+        cdef double d
+        memcpy(&d, &asint, sizeof(asint))
+        return PyFloat_FromDouble(d)
 
 
 @cython.final