From b19daebeef209b170267ca62ed48249dab387e2f Mon Sep 17 00:00:00 2001 From: Misa Date: Mon, 15 Feb 2021 19:00:18 -0800 Subject: [PATCH] Bail for all SDL_malloc() failures Following Ethan's example of bailing (calling VVV_exit()) if binaryBlob::unPackBinary() couldn't allocate memory, I've searched through and found every SDL_malloc(), then made sure that if it returned NULL, the caller would bail (because you can't do much when you're out of memory). There should probably be an error message printed when the process is out of memory, but unPackBinary() doesn't print an error message for being out of memory, so this can probably be added later. (Also we don't really have a logging system, I'd like to have something like that added in first before adding more messages.) Also, this doesn't account for any allocators used by STL stuff, but we're working on removing the STL, and allocation failure just results in an abort anyway, so there's not really a problem there. --- desktop_version/src/BinaryBlob.cpp | 4 ++++ desktop_version/src/FileSystemUtils.cpp | 18 +++++++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/desktop_version/src/BinaryBlob.cpp b/desktop_version/src/BinaryBlob.cpp index 0850e4bd..01abcead 100644 --- a/desktop_version/src/BinaryBlob.cpp +++ b/desktop_version/src/BinaryBlob.cpp @@ -28,6 +28,10 @@ void binaryBlob::AddFileToBinaryBlob(const char* _path) fseek(file, 0, SEEK_SET); memblock = (char*) SDL_malloc(size); + if (memblock == NULL) + { + VVV_exit(1); + } fread(memblock, 1, size, file); fclose(file); diff --git a/desktop_version/src/FileSystemUtils.cpp b/desktop_version/src/FileSystemUtils.cpp index 074460ed..cc8a774c 100644 --- a/desktop_version/src/FileSystemUtils.cpp +++ b/desktop_version/src/FileSystemUtils.cpp @@ -4,11 +4,11 @@ #include #include #include -#include #include #include #include +#include "Exit.h" #include "Graphics.h" #include "UtilityClass.h" @@ -259,6 +259,10 @@ void FILESYSTEM_loadFileToMemory( ++length; *mem = static_cast(SDL_malloc(length)); // STDIN_BUFFER.data() causes double-free + if (*mem == NULL) + { + VVV_exit(1); + } std::copy(STDIN_BUFFER.begin(), STDIN_BUFFER.end(), reinterpret_cast(*mem)); return; } @@ -276,11 +280,19 @@ void FILESYSTEM_loadFileToMemory( if (addnull) { *mem = (unsigned char *) SDL_malloc(length + 1); + if (*mem == NULL) + { + VVV_exit(1); + } (*mem)[length] = 0; } else { *mem = (unsigned char*) SDL_malloc(length); + if (*mem == NULL) + { + VVV_exit(1); + } } int success = PHYSFS_readBytes(handle, *mem, length); if (success == -1) @@ -495,6 +507,10 @@ static void PLATFORM_copyFile(const char *oldLocation, const char *newLocation) length = ftell(file); fseek(file, 0, SEEK_SET); data = (char*) SDL_malloc(length); + if (data == NULL) + { + VVV_exit(1); + } bytes_read = fread(data, 1, length, file); fclose(file); if (bytes_read != length)