1
0
mirror of https://github.com/TerryCavanagh/VVVVVV.git synced 2024-06-02 02:53:32 +02:00

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.
This commit is contained in:
Misa 2021-02-15 19:00:18 -08:00 committed by Ethan Lee
parent 8aa5bb8aab
commit b19daebeef
2 changed files with 21 additions and 1 deletions

View File

@ -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);

View File

@ -4,11 +4,11 @@
#include <physfs.h>
#include <SDL.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <tinyxml2.h>
#include <vector>
#include "Exit.h"
#include "Graphics.h"
#include "UtilityClass.h"
@ -259,6 +259,10 @@ void FILESYSTEM_loadFileToMemory(
++length;
*mem = static_cast<unsigned char*>(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<char*>(*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)