From a0a1d102a0c2957f7dcb0837902803b6f3e3b2f1 Mon Sep 17 00:00:00 2001 From: Sebastian Wick Date: Mon, 8 Jan 2024 16:08:37 +0100 Subject: [PATCH] cogl/bitmap-conversion: Don't break strict-aliasing for flt_pack/unpack Simply reinterpreting the bytes differently is a strict-aliasing violation if the type of the object isn't char or the target type of the reinterpretation. None of that is the case here, so we have to resort to a memcpy. Fixes: 60c082caa ("cogl/bitmap-conversion: Support packing fp16 formats") Part-of: --- cogl/cogl/cogl-bitmap-conversion.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/cogl/cogl/cogl-bitmap-conversion.c b/cogl/cogl/cogl-bitmap-conversion.c index 76efbb9bb..948e633ac 100644 --- a/cogl/cogl/cogl-bitmap-conversion.c +++ b/cogl/cogl/cogl-bitmap-conversion.c @@ -45,16 +45,22 @@ typedef enum MEDIUM_TYPE_FLOAT, } MediumType; +G_STATIC_ASSERT (sizeof (uint32_t) == sizeof (GLfloat)); + inline static uint32_t pack_flt (GLfloat b) { - return *(uint32_t *) &b; + uint32_t ret; + memcpy (&ret, &b, sizeof (uint32_t)); + return ret; } inline static GLfloat unpack_flt (uint32_t b) { - return *(GLfloat *) &b; + GLfloat ret; + memcpy (&ret, &b, sizeof (GLfloat)); + return ret; } #define CLAMP_NORM(b) (MAX (MIN ((b), 1.0), 0.0))