From 9e4e8ae1e68053fe8e07783ff694b6f101a5c5b3 Mon Sep 17 00:00:00 2001 From: Andreas Rheinhardt Date: Fri, 29 Mar 2024 02:22:06 +0100 Subject: [PATCH] avcodec/pngdsp: Fix unaligned accesses, effective type violations Affected the lscr fate-test (only visible on x86 if the SSE2 is disabled). Signed-off-by: Andreas Rheinhardt --- libavcodec/pngdsp.c | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/libavcodec/pngdsp.c b/libavcodec/pngdsp.c index 65916b1386..50ee96a684 100644 --- a/libavcodec/pngdsp.c +++ b/libavcodec/pngdsp.c @@ -21,20 +21,33 @@ #include "config.h" #include "libavutil/attributes.h" +#include "libavutil/intreadwrite.h" +#include "libavutil/macros.h" #include "png.h" #include "pngdsp.h" +#if HAVE_FAST_64BIT +#define BITS 64 +typedef uint64_t uint_native; +#else +#define BITS 32 +typedef uint32_t uint_native; +#endif +#define RN AV_JOIN(AV_RN, BITS) +#define RNA AV_JOIN(AV_JOIN(AV_RN, BITS), A) +#define WN AV_JOIN(AV_WN, BITS) + // 0x7f7f7f7f or 0x7f7f7f7f7f7f7f7f or whatever, depending on the cpu's native arithmetic size -#define pb_7f (~0UL / 255 * 0x7f) -#define pb_80 (~0UL / 255 * 0x80) +#define pb_7f (~(uint_native)0 / 255 * 0x7f) +#define pb_80 (~(uint_native)0 / 255 * 0x80) static void add_bytes_l2_c(uint8_t *dst, uint8_t *src1, uint8_t *src2, int w) { long i; - for (i = 0; i <= w - (int) sizeof(long); i += sizeof(long)) { - long a = *(long *)(src1 + i); - long b = *(long *)(src2 + i); - *(long *)(dst + i) = ((a & pb_7f) + (b & pb_7f)) ^ ((a ^ b) & pb_80); + for (i = 0; i <= w - (int) sizeof(uint_native); i += sizeof(uint_native)) { + uint_native a = RNA(src1 + i); + uint_native b = RN (src2 + i); + WN(dst + i, ((a & pb_7f) + (b & pb_7f)) ^ ((a ^ b) & pb_80)); } for (; i < w; i++) dst[i] = src1[i] + src2[i];