From 79f5e7a05cdfba76b654d28ba71427234dbaedc8 Mon Sep 17 00:00:00 2001 From: Dav999-v Date: Thu, 16 Mar 2023 21:45:25 +0100 Subject: [PATCH] Fix loading font .txt files that contain null bytes An example is Maximally Misleading Miserable Misadventure, which has a font.txt which includes all ASCII characters starting with a 0x00 byte. This would accidentally null-terminate the string too early. Instead, we now use the total length of the file again, and keep getting the next UTF-8 codepoint until the file ends. We still need to null-terminate it - it protects against incomplete sequences getting the UTF-8 decoder to read out of bounds. --- desktop_version/src/Font.cpp | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/desktop_version/src/Font.cpp b/desktop_version/src/Font.cpp index 1c597433..1e03477a 100644 --- a/desktop_version/src/Font.cpp +++ b/desktop_version/src/Font.cpp @@ -344,18 +344,22 @@ static uint8_t load_font(FontContainer* container, const char* name) bool charset_loaded = false; bool special_loaded = false; unsigned char* charmap = NULL; + size_t length; if (FILESYSTEM_areAssetsInSameRealDir(name_png, name_txt)) { - FILESYSTEM_loadAssetToMemory(name_txt, &charmap, NULL, true); + /* The .txt can contain null bytes, but it's still null-terminated - it protects + * against incomplete sequences getting the UTF-8 decoder to read out of bounds. */ + FILESYSTEM_loadAssetToMemory(name_txt, &charmap, &length, true); } if (charmap != NULL) { // We have a .txt! It's an obsolete system, but it takes priority if the file exists. const char* current = (char*) charmap; + const char* end = (char*) charmap + length; int pos = 0; - uint32_t codepoint; - while ((codepoint = UTF8_next(¤t))) + while (current < end) { + uint32_t codepoint = UTF8_next(¤t); add_glyphinfo(f, codepoint, pos); ++pos; }