https://github.com/amadvance/advancecomp/commit/916501836f61ad1beedd943f40ff6f324181fe1d https://sourceforge.net/p/advancemame/bugs/311/ https://bugs.gentoo.org/959422 From 916501836f61ad1beedd943f40ff6f324181fe1d Mon Sep 17 00:00:00 2001 From: Andrea Mazzoleni Date: Mon, 7 Jul 2025 14:11:09 +0200 Subject: [PATCH] Fix misaligned access in vectorized comparison by adding runtime alignment checks --- zopfli/lz77.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/zopfli/lz77.c b/zopfli/lz77.c index 9df899d..adb3ee4 100644 --- a/zopfli/lz77.c +++ b/zopfli/lz77.c @@ -24,6 +24,7 @@ Author: jyrki.alakuijala@gmail.com (Jyrki Alakuijala) #include #include #include +#include void ZopfliInitLZ77Store(const unsigned char* data, ZopfliLZ77Store* store) { store->size = 0; @@ -299,13 +300,13 @@ static const unsigned char* GetMatch(const unsigned char* scan, const unsigned char* end, const unsigned char* safe_end) { - if (sizeof(size_t) == 8) { + if (sizeof(size_t) == 8 && ((uintptr_t)scan % 8 == 0) && ((uintptr_t)match % 8 == 0)) { /* 8 checks at once per array bounds check (size_t is 64-bit). */ while (scan < safe_end && *((size_t*)scan) == *((size_t*)match)) { scan += 8; match += 8; } - } else if (sizeof(unsigned int) == 4) { + } else if (sizeof(unsigned int) == 4 && ((uintptr_t)scan % 4 == 0) && ((uintptr_t)match % 4 == 0)) { /* 4 checks at once per array bounds check (unsigned int is 32-bit). */ while (scan < safe_end && *((unsigned int*)scan) == *((unsigned int*)match)) {