From 5862af4445bfd9c056de7ef9694f8cb31f9d469a Mon Sep 17 00:00:00 2001 From: Fredrik Ljungdahl Date: Fri, 24 Jan 2020 21:35:46 +0100 Subject: [PATCH] Add a null terminator to loaded TinyXML files (#117) * Add a null terminator to loaded TinyXML files The TinyXML parse() function expect a C-like string, including terminator. When xml loading was changed, it loaded the file, but included no such thing. Thus, we load the file, then reallocate the memory so that we can insert a null terminator to it, before passing it to parse(). * Tweak TinyXML file loading Instead of first loading the file content into memory, then reallocate it to add a null pointer, add an argument to the file load function for whether to append a null terminator or not, defaulting to false. It still returns the length without the null pointer in case a length ptr is passed. --- desktop_version/src/FileSystemUtils.cpp | 15 ++++++++++++--- desktop_version/src/FileSystemUtils.h | 3 ++- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/desktop_version/src/FileSystemUtils.cpp b/desktop_version/src/FileSystemUtils.cpp index 69b0cf75..f0fa1ce4 100644 --- a/desktop_version/src/FileSystemUtils.cpp +++ b/desktop_version/src/FileSystemUtils.cpp @@ -123,7 +123,8 @@ char *FILESYSTEM_getUserLevelDirectory() return levelDir; } -void FILESYSTEM_loadFileToMemory(const char *name, unsigned char **mem, size_t *len) +void FILESYSTEM_loadFileToMemory(const char *name, unsigned char **mem, + size_t *len, bool addnull) { PHYSFS_File *handle = PHYSFS_openRead(name); if (handle == NULL) @@ -135,7 +136,15 @@ void FILESYSTEM_loadFileToMemory(const char *name, unsigned char **mem, size_t * { *len = length; } - *mem = (unsigned char*) malloc(length); + if (addnull) + { + *mem = (unsigned char *) malloc(length + 1); + mem[length] = 0; + } + else + { + *mem = (unsigned char*) malloc(length); + } PHYSFS_readBytes(handle, *mem, length); PHYSFS_close(handle); } @@ -165,7 +174,7 @@ bool FILESYSTEM_loadTiXmlDocument(const char *name, TiXmlDocument *doc) { /* TiXmlDocument.SaveFile doesn't account for Unicode paths, PHYSFS does */ unsigned char *mem = NULL; - FILESYSTEM_loadFileToMemory(name, &mem, NULL); + FILESYSTEM_loadFileToMemory(name, &mem, NULL, true); if (mem == NULL) { return false; diff --git a/desktop_version/src/FileSystemUtils.h b/desktop_version/src/FileSystemUtils.h index 71274daf..d3c551e2 100644 --- a/desktop_version/src/FileSystemUtils.h +++ b/desktop_version/src/FileSystemUtils.h @@ -12,7 +12,8 @@ void FILESYSTEM_deinit(); char *FILESYSTEM_getUserSaveDirectory(); char *FILESYSTEM_getUserLevelDirectory(); -void FILESYSTEM_loadFileToMemory(const char *name, unsigned char **mem, size_t *len); +void FILESYSTEM_loadFileToMemory(const char *name, unsigned char **mem, + size_t *len, bool addnull = false); void FILESYSTEM_freeMemory(unsigned char **mem); bool FILESYSTEM_saveTiXmlDocument(const char *name, TiXmlDocument *doc); bool FILESYSTEM_loadTiXmlDocument(const char *name, TiXmlDocument *doc);