From 8c8118e43f7cca472539d644b69e616322353008 Mon Sep 17 00:00:00 2001 From: Misa Date: Sun, 18 Apr 2021 10:29:23 -0700 Subject: [PATCH] Don't mix code and decls in loadFileToMemory() My next commit will involve using goto to jump to the end of a function to initialize the variables to NULL, but that results in a compiler error if we have initializations in the middle of the function. We might as well put all declarations at the top of each block anyway, to help the move to C, so I'm doing this now. Since the length variable in the STDIN block now overshadows the length variable in the outer block, I've renamed the length variable in the block to stdin_length. --- desktop_version/src/FileSystemUtils.cpp | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/desktop_version/src/FileSystemUtils.cpp b/desktop_version/src/FileSystemUtils.cpp index 44162aee..f077d38f 100644 --- a/desktop_version/src/FileSystemUtils.cpp +++ b/desktop_version/src/FileSystemUtils.cpp @@ -447,11 +447,16 @@ void FILESYSTEM_loadFileToMemory( size_t *len, bool addnull ) { + PHYSFS_File *handle; + PHYSFS_sint64 length; + PHYSFS_sint64 success; + if (SDL_strcmp(name, "levels/special/stdin.vvvvvv") == 0) { // this isn't *technically* necessary when piping directly from a file, but checking for that is annoying static std::vector STDIN_BUFFER; static bool STDIN_LOADED = false; + size_t stdin_length; if (!STDIN_LOADED) { std::istreambuf_iterator begin(std::cin), end; @@ -460,14 +465,14 @@ void FILESYSTEM_loadFileToMemory( STDIN_LOADED = true; } - size_t length = STDIN_BUFFER.size() - 1; + stdin_length = STDIN_BUFFER.size() - 1; if (len != NULL) { - *len = length; + *len = stdin_length; } - ++length; - *mem = static_cast(SDL_malloc(length)); // STDIN_BUFFER.data() causes double-free + ++stdin_length; + *mem = static_cast(SDL_malloc(stdin_length)); // STDIN_BUFFER.data() causes double-free if (*mem == NULL) { VVV_exit(1); @@ -476,12 +481,12 @@ void FILESYSTEM_loadFileToMemory( return; } - PHYSFS_File *handle = PHYSFS_openRead(name); + handle = PHYSFS_openRead(name); if (handle == NULL) { return; } - PHYSFS_sint64 length = PHYSFS_fileLength(handle); + length = PHYSFS_fileLength(handle); if (len != NULL) { if (length < 0) @@ -507,7 +512,7 @@ void FILESYSTEM_loadFileToMemory( VVV_exit(1); } } - PHYSFS_sint64 success = PHYSFS_readBytes(handle, *mem, length); + success = PHYSFS_readBytes(handle, *mem, length); if (success == -1) { FILESYSTEM_freeMemory(mem);