From 2028e19e67a4875448ba25ae5a4e962c0e41b93f Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Thu, 25 Jul 2024 11:57:02 +0200 Subject: [PATCH] refactor(c): avoid violating strict aliasing rule in interval binary loader See conversation in #877 --- psycopg_c/psycopg_c/types/datetime.pyx | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/psycopg_c/psycopg_c/types/datetime.pyx b/psycopg_c/psycopg_c/types/datetime.pyx index 44b227eda..e2a6ac8a3 100644 --- a/psycopg_c/psycopg_c/types/datetime.pyx +++ b/psycopg_c/psycopg_c/types/datetime.pyx @@ -971,11 +971,13 @@ cdef class IntervalBinaryLoader(CLoader): format = PQ_BINARY cdef object cload(self, const char *data, size_t length): - cdef int64_t bedata[2] - memcpy(&bedata, data, sizeof(bedata)) - cdef int64_t val = endian.be64toh(bedata[0]) - cdef int32_t days = endian.be32toh((bedata)[2]) - cdef int32_t months = endian.be32toh((bedata)[3]) + cdef int64_t beval + cdef int32_t bedm[2] + memcpy(&beval, data, sizeof(beval)) + memcpy(bedm, data + sizeof(beval), sizeof(bedm)) + cdef int64_t val = endian.be64toh(beval) + cdef int32_t days = endian.be32toh(bedm[0]) + cdef int32_t months = endian.be32toh(bedm[1]) cdef int years with cython.cdivision(True): -- 2.47.2