diff mbox series

[FFmpeg-devel,3/5] avcodec/pngdsp: Fix unaligned accesses, effective type violations

Message ID GV1P250MB0737D370417DDB5BBD2413048F3A2@GV1P250MB0737.EURP250.PROD.OUTLOOK.COM
State Accepted
Commit 9e4e8ae1e68053fe8e07783ff694b6f101a5c5b3
Headers show
Series [FFmpeg-devel,1/5] avcodec/ppc/hpeldsp_altivec: Fix left-shift of negative number | expand

Checks

Context Check Description
yinshiyou/make_loongarch64 success Make finished
yinshiyou/make_fate_loongarch64 success Make fate finished
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished

Commit Message

Andreas Rheinhardt March 29, 2024, 3:24 a.m. UTC
Affected the lscr fate-test (only visible on x86 if
the SSE2 is disabled).

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 libavcodec/pngdsp.c | 25 +++++++++++++++++++------
 1 file changed, 19 insertions(+), 6 deletions(-)
diff mbox series

Patch

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];