Skip to content

Commit

Permalink
Fixed bug #324 - prevent unaligned load access
Browse files Browse the repository at this point in the history
use memcpy so we can expect the compiler to optimize when possible

(cherry picked from commit d2e6ded)
  • Loading branch information
1bsyl authored and slouken committed Jan 10, 2024
1 parent 073da22 commit f75fb29
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions SDL_ttf.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit f75fb29

Please sign in to comment.