From f75fb299fde157d6edb57d2c272009ce22d2569d Mon Sep 17 00:00:00 2001 From: Sylvain Date: Tue, 9 Jan 2024 15:38:24 +0100 Subject: [PATCH] Fixed bug #324 - prevent unaligned load access use memcpy so we can expect the compiler to optimize when possible (cherry picked from commit d2e6ded172d88b21e0ca6f3a198353c18bdd3eb4) --- SDL_ttf.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/SDL_ttf.c b/SDL_ttf.c index 1a3ebccf..36c834c9 100644 --- a/SDL_ttf.c +++ b/SDL_ttf.c @@ -416,7 +416,9 @@ static SDL_INLINE void BG_Blended_Color(const TTF_Image *image, Uint32 *destinat while (height--) { /* *INDENT-OFF* */ DUFFS_LOOP4( - tmp = *src++; + /* prevent misaligned load: tmp = *src++; */ + /* eventually, we can expect the compiler to replace the memcpy call with something optimized */ + memcpy(&tmp, src++, sizeof(tmp)); /* This should NOT be SDL_memcpy */ alpha = tmp >> 24; tmp &= ~0xFF000000; alpha = fg_alpha * alpha; @@ -451,7 +453,8 @@ static SDL_INLINE void BG_Blended_LCD(const TTF_Image *image, Uint32 *destinatio while (height--) { /* *INDENT-OFF* */ DUFFS_LOOP4( - tmp = *src++; + /* prevent misaligned load: tmp = *src++; */ + memcpy(&tmp, src++, sizeof(tmp)); /* This should NOT be SDL_memcpy */ if (tmp) { bg = *dst; @@ -908,11 +911,14 @@ static SDL_INLINE void BG_64(const TTF_Image *image, Uint8 *destination, Sint32 Uint64 *dst = (Uint64 *)destination; Uint32 width = image->width / 8; Uint32 height = image->rows; + Uint64 tmp; while (height--) { /* *INDENT-OFF* */ DUFFS_LOOP4( - *dst++ |= *src++; + /* prevent misaligned load: *dst++ |= *src++; */ + memcpy(&tmp, src++, sizeof(tmp)); /* This should NOT be SDL_memcpy */ + *dst++ |= tmp; , width); /* *INDENT-ON* */ src = (const Uint64 *)((const Uint8 *)src + srcskip); @@ -926,11 +932,14 @@ static SDL_INLINE void BG_32(const TTF_Image *image, Uint8 *destination, Sint32 Uint32 *dst = (Uint32 *)destination; Uint32 width = image->width / 4; Uint32 height = image->rows; + Uint32 tmp; while (height--) { /* *INDENT-OFF* */ DUFFS_LOOP4( - *dst++ |= *src++; + /* prevent misaligned load: *dst++ |= *src++; */ + memcpy(&tmp, src++, sizeof(tmp)); /* This should NOT be SDL_memcpy */ + *dst++ |= tmp; , width); /* *INDENT-ON* */ src = (const Uint32 *)((const Uint8 *)src + srcskip);