From df577c59ef16df8893e0628559de1b304e84b951 Mon Sep 17 00:00:00 2001 From: Dav999-v Date: Thu, 16 Mar 2023 18:55:46 +0100 Subject: [PATCH] Fix font .txt files not being null-terminated This was an oversight when we migrated to the new UTF-8 system - it expects a null-terminated string, but the utfcpp implementation worked with a pointer to the end of the file instead. I also added an assert in FILESYSTEM_loadFileToMemory() so this is less likely to happen again - because there should be no valid reason to have a NULL pointer for the total file size, as well as not wanting a null terminator to be added at the end of the file. --- desktop_version/src/FileSystemUtils.cpp | 6 ++++++ desktop_version/src/Font.cpp | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/desktop_version/src/FileSystemUtils.cpp b/desktop_version/src/FileSystemUtils.cpp index 4b49ce64..3a706de1 100644 --- a/desktop_version/src/FileSystemUtils.cpp +++ b/desktop_version/src/FileSystemUtils.cpp @@ -741,6 +741,12 @@ void FILESYSTEM_loadFileToMemory( goto fail; } + if (len == NULL && !addnull) + { + vlog_warn("%s is loaded with len == NULL && !addnull", name); + SDL_assert(0 && "Are you sure you don't want a null terminator to be added to these loaded file contents?"); + } + /* FIXME: Dumb hack to use `special/stdin.vvvvvv` here... * This is also checked elsewhere... grep for `special/stdin`! */ if (SDL_strcmp(name, "levels/special/stdin.vvvvvv") == 0) diff --git a/desktop_version/src/Font.cpp b/desktop_version/src/Font.cpp index 0d7c81c2..1c597433 100644 --- a/desktop_version/src/Font.cpp +++ b/desktop_version/src/Font.cpp @@ -346,7 +346,7 @@ static uint8_t load_font(FontContainer* container, const char* name) unsigned char* charmap = NULL; if (FILESYSTEM_areAssetsInSameRealDir(name_png, name_txt)) { - FILESYSTEM_loadAssetToMemory(name_txt, &charmap, NULL, false); + FILESYSTEM_loadAssetToMemory(name_txt, &charmap, NULL, true); } if (charmap != NULL) {