1
0
Fork 0
mirror of https://github.com/TerryCavanagh/VVVVVV.git synced 2024-12-23 01:59:43 +01:00

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.
This commit is contained in:
Dav999-v 2023-03-16 21:45:25 +01:00 committed by Misa Elizabeth Kai
parent df577c59ef
commit 79f5e7a05c

View file

@ -344,18 +344,22 @@ static uint8_t load_font(FontContainer* container, const char* name)
bool charset_loaded = false; bool charset_loaded = false;
bool special_loaded = false; bool special_loaded = false;
unsigned char* charmap = NULL; unsigned char* charmap = NULL;
size_t length;
if (FILESYSTEM_areAssetsInSameRealDir(name_png, name_txt)) 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) if (charmap != NULL)
{ {
// We have a .txt! It's an obsolete system, but it takes priority if the file exists. // We have a .txt! It's an obsolete system, but it takes priority if the file exists.
const char* current = (char*) charmap; const char* current = (char*) charmap;
const char* end = (char*) charmap + length;
int pos = 0; int pos = 0;
uint32_t codepoint; while (current < end)
while ((codepoint = UTF8_next(&current)))
{ {
uint32_t codepoint = UTF8_next(&current);
add_glyphinfo(f, codepoint, pos); add_glyphinfo(f, codepoint, pos);
++pos; ++pos;
} }