From 75ee6576120637b99d6d73a71b52d19a9a140f49 Mon Sep 17 00:00:00 2001 From: Misa Date: Sat, 12 Mar 2022 16:50:32 -0800 Subject: [PATCH] Explicitly prevent writing to saves if filesystem is not init Another cause of #870 is d0ffafe117e143023cb9774d651f5cd9a635c5e8, as a bisect tells me. What that commit did is remove screenbuffer as a pointer, since it's a statically-allocated object that _should_ always exist, and it removed the `screenbuffer == NULL` guards in savestats() and savesettings(). Unfortunately, those guards did something very important - namely, they prevented writing to the save files when the filesystem wasn't initialized. But that wasn't made clear, because it seemed like the point of those guards was to prevent dereferencing NULL. So instead, explicitly make it clear that FILESYSTEM_saveTiXml2Document() needs to fail if the filesystem isn't initialized. I've done this by adding an isInit bool to FileSystemUtils.cpp. --- desktop_version/src/FileSystemUtils.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/desktop_version/src/FileSystemUtils.cpp b/desktop_version/src/FileSystemUtils.cpp index 04428ab4..0d352d57 100644 --- a/desktop_version/src/FileSystemUtils.cpp +++ b/desktop_version/src/FileSystemUtils.cpp @@ -34,6 +34,8 @@ static int mkdir(char* path, int mode) #define MAX_PATH PATH_MAX #endif +static bool isInit = false; + static const char* pathSep = NULL; static char* basePath = NULL; static char saveDir[MAX_PATH] = {'\0'}; @@ -179,6 +181,8 @@ int FILESYSTEM_init(char *argvZero, char* baseDir, char *assetsPath) { vlog_info("gamecontrollerdb.txt not found!"); } + + isInit = true; return 1; } @@ -198,6 +202,7 @@ void FILESYSTEM_deinit(void) } SDL_free(basePath); basePath = NULL; + isInit = false; } char *FILESYSTEM_getUserSaveDirectory(void) @@ -910,6 +915,12 @@ fail: bool FILESYSTEM_saveTiXml2Document(const char *name, tinyxml2::XMLDocument& doc, bool sync /*= true*/) { + if (!isInit) + { + vlog_warn("Filesystem not initialized! Not writing just to be safe."); + return false; + } + /* XMLDocument.SaveFile doesn't account for Unicode paths, PHYSFS does */ tinyxml2::XMLPrinter printer; doc.Print(&printer);