From 43e293c138e53a258385bbbd058b619324a9ca1a Mon Sep 17 00:00:00 2001 From: 94xhn <87560781+94xhn@users.noreply.github.com> Date: Sat, 18 Jul 2026 14:13:48 +0800 Subject: [PATCH] Preserve wide custom-font ranges on big-endian targets GFXfont stores first and last as uint16_t, but the custom-font paths read them as bytes. Use word-width reads while retaining the existing 8-bit character API, so the stored range is interpreted correctly without changing the font format or adding UTF-8 behavior. Constraint: Keep the public 8-bit character API and limit the patch to issue #490\nRejected: UTF-8/API refactor | broader scope is outside the reported portability bug\nConfidence: high\nScope-risk: narrow\nReversibility: clean\nDirective: Keep GFXfont first/last accesses word-width when reading the stored range\nTested: MinGW GCC 8 -Wall -Wextra -Werror with an endian-aware host fixture; WSL GCC AddressSanitizer and UBSan; baseline fixture fails with 0x0 bounds while the fix passes with 5x7\nNot-tested: Physical Arduino board hardware --- Adafruit_GFX.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Adafruit_GFX.cpp b/Adafruit_GFX.cpp index ae26bf1..9032f2c 100644 --- a/Adafruit_GFX.cpp +++ b/Adafruit_GFX.cpp @@ -1435,7 +1435,7 @@ void Adafruit_GFX::drawChar(int16_t x, int16_t y, unsigned char c, // newlines, returns, non-printable characters, etc. Calling // drawChar() directly with 'bad' characters of font may cause mayhem! - c -= (uint8_t)pgm_read_byte(&gfxFont->first); + c -= (uint8_t)pgm_read_word(&gfxFont->first); GFXglyph *glyph = pgm_read_glyph_ptr(gfxFont, c); uint8_t *bitmap = pgm_read_bitmap_ptr(gfxFont); @@ -1519,8 +1519,8 @@ size_t Adafruit_GFX::write(uint8_t c) { cursor_y += (int16_t)textsize_y * (uint8_t)pgm_read_byte(&gfxFont->yAdvance); } else if (c != '\r') { - uint8_t first = pgm_read_byte(&gfxFont->first); - if ((c >= first) && (c <= (uint8_t)pgm_read_byte(&gfxFont->last))) { + uint16_t first = pgm_read_word(&gfxFont->first); + if ((c >= first) && (c <= pgm_read_word(&gfxFont->last))) { GFXglyph *glyph = pgm_read_glyph_ptr(gfxFont, c - first); uint8_t w = pgm_read_byte(&glyph->width), h = pgm_read_byte(&glyph->height); @@ -1635,8 +1635,8 @@ void Adafruit_GFX::charBounds(unsigned char c, int16_t *x, int16_t *y, *x = 0; // Reset x to zero, advance y by one line *y += textsize_y * (uint8_t)pgm_read_byte(&gfxFont->yAdvance); } else if (c != '\r') { // Not a carriage return; is normal char - uint8_t first = pgm_read_byte(&gfxFont->first), - last = pgm_read_byte(&gfxFont->last); + uint16_t first = pgm_read_word(&gfxFont->first), + last = pgm_read_word(&gfxFont->last); if ((c >= first) && (c <= last)) { // Char present in this font? GFXglyph *glyph = pgm_read_glyph_ptr(gfxFont, c - first); uint8_t gw = pgm_read_byte(&glyph->width),