From 31bf816b1b45dcdcaa8a7313267194d426c0bd13 Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Wed, 24 Jul 2024 10:37:30 +0200 Subject: [PATCH] refactor(c): more idiomatic type-punned pointer warning workaround We are using memcpy in all the other cases. --- psycopg_c/psycopg_c/types/numeric.pyx | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/psycopg_c/psycopg_c/types/numeric.pyx b/psycopg_c/psycopg_c/types/numeric.pyx index ff4dacec2..31dc75b3e 100644 --- a/psycopg_c/psycopg_c/types/numeric.pyx +++ b/psycopg_c/psycopg_c/types/numeric.pyx @@ -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 = &asint - return PyFloat_FromDouble((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 = &asint - return PyFloat_FromDouble((swp)[0]) + cdef double d + memcpy(&d, &asint, sizeof(asint)) + return PyFloat_FromDouble(d) @cython.final -- 2.39.5