Replace all free calls with VVV_free[func]

This replaces all calls to SDL_free with a new macro, VVV_free, that
nulls the pointer afterwards. This mitigates any use-after-frees and
also completely eliminates double-frees. The same is done for any
function to free specific objects such as SDL_FreeSurface, with the
VVV_freefunc macro.

No exceptions for any of these calls, even if the pointer is discarded
or zeroed afterwards anyway. Better safe than sorry.

This is a macro rather than a function that takes in a
pointer-to-pointer because such a function would have type issues that
require casting and that's just not safe.

Even though SDL_free and other SDL functions already check for NULL, the
macro has a NULL check for other functions that don't. For example,
FAudioVoice_DestroyVoice does not check for NULL.

FILESYSTEM_freeMemory has been axed in favor of VVV_free because it
functionally does the same thing except for `unsigned char*` only.
This commit is contained in:
Misa
2022-11-30 22:30:16 -08:00
parent 6e583d949b
commit a926ce9851
15 changed files with 95 additions and 100 deletions

View File

@@ -4,6 +4,7 @@
#include <stdio.h>
#include <tinyxml2.h>
#include "Alloc.h"
#include "BinaryBlob.h"
#include "Exit.h"
#include "Graphics.h"
@@ -201,13 +202,8 @@ void FILESYSTEM_deinit(void)
{
PHYSFS_deinit();
}
if (stdin_buffer != NULL)
{
SDL_free(stdin_buffer);
stdin_buffer = NULL;
}
SDL_free(basePath);
basePath = NULL;
VVV_free(stdin_buffer);
VVV_free(basePath);
isInit = false;
}
@@ -505,8 +501,6 @@ bool FILESYSTEM_isAssetMounted(const char* filename)
return SDL_strcmp(assetDir, realDir) == 0;
}
void FILESYSTEM_freeMemory(unsigned char **mem);
static void load_stdin(void)
{
size_t pos = 0;
@@ -636,7 +630,7 @@ void FILESYSTEM_loadFileToMemory(
success = PHYSFS_readBytes(handle, *mem, length);
if (success == -1)
{
FILESYSTEM_freeMemory(mem);
VVV_free(*mem);
}
PHYSFS_close(handle);
return;
@@ -665,12 +659,6 @@ void FILESYSTEM_loadAssetToMemory(
FILESYSTEM_loadFileToMemory(path, mem, len, addnull);
}
void FILESYSTEM_freeMemory(unsigned char **mem)
{
SDL_free(*mem);
*mem = NULL;
}
bool FILESYSTEM_loadBinaryBlob(binaryBlob* blob, const char* filename)
{
PHYSFS_sint64 size;
@@ -817,7 +805,7 @@ bool FILESYSTEM_loadTiXml2Document(const char *name, tinyxml2::XMLDocument& doc)
return false;
}
doc.Parse((const char*) mem);
FILESYSTEM_freeMemory(&mem);
VVV_free(mem);
return true;
}