Skip to content

Commit

Permalink
Explicitly prevent writing to saves if filesystem is not init
Browse files Browse the repository at this point in the history
Another cause of #870 is d0ffafe, 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.
  • Loading branch information
InfoTeddy committed Mar 13, 2022
1 parent 997363c commit 75ee657
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions desktop_version/src/FileSystemUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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'};
Expand Down Expand Up @@ -179,6 +181,8 @@ int FILESYSTEM_init(char *argvZero, char* baseDir, char *assetsPath)
{
vlog_info("gamecontrollerdb.txt not found!");
}

isInit = true;
return 1;
}

Expand All @@ -198,6 +202,7 @@ void FILESYSTEM_deinit(void)
}
SDL_free(basePath);
basePath = NULL;
isInit = false;
}

char *FILESYSTEM_getUserSaveDirectory(void)
Expand Down Expand Up @@ -910,6 +915,12 @@ bool FILESYSTEM_loadBinaryBlob(binaryBlob* blob, const char* filename)

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);
Expand Down

0 comments on commit 75ee657

Please sign in to comment.