1
0
Fork 0
mirror of https://github.com/TerryCavanagh/VVVVVV.git synced 2024-12-23 01:59:43 +01:00

Transfer param init responsibility to loadFileToMemory

So, the codebase was kind of undecided about who is responsible for
initializing the parameters passed to FILESYSTEM_loadFileToMemory() - is
it the caller? Is it FILESYSTEM_loadFileToMemory()? Sometimes callers
would initialize one variable but not the other, and it was always a
toss-up whether or not FILESYSTEM_loadFileToMemory() would end up
initializing everything in the end.

All of this is to say that the game dereferences an uninitialized
pointer if it can't load a sound effect. Which is bad. Now, I could
either fix that single case, or fix every case. Judging by the title of
this commit, you can infer that I decided to fix every case - fixing
every case means not just all cases that currently exist (which, as far
as I know, is only the sound effect one), but all cases that could exist
in the future.

So, FILESYSTEM_loadFileToMemory() is now guaranteed to initialize its
parameters even if the file fails to be loaded. This is better than
passing the responsibility to the caller anyway, because if the caller
initialized it, then that would be wasted work if the file succeeds
anyway because FILESYSTEM_loadFileToMemory() will overwrite it, and if
the file fails to load, well that's when the variables get initialized
anyway.
This commit is contained in:
Misa 2021-04-18 10:35:36 -07:00 committed by Ethan Lee
parent 8c8118e43f
commit 3ebdc1da89
6 changed files with 20 additions and 9 deletions

View file

@ -484,7 +484,7 @@ void FILESYSTEM_loadFileToMemory(
handle = PHYSFS_openRead(name); handle = PHYSFS_openRead(name);
if (handle == NULL) if (handle == NULL)
{ {
return; goto fail;
} }
length = PHYSFS_fileLength(handle); length = PHYSFS_fileLength(handle);
if (len != NULL) if (len != NULL)
@ -518,6 +518,17 @@ void FILESYSTEM_loadFileToMemory(
FILESYSTEM_freeMemory(mem); FILESYSTEM_freeMemory(mem);
} }
PHYSFS_close(handle); PHYSFS_close(handle);
return;
fail:
if (mem != NULL)
{
*mem = NULL;
}
if (len != NULL)
{
*len = 0;
}
} }
void FILESYSTEM_loadAssetToMemory( void FILESYSTEM_loadAssetToMemory(
@ -645,7 +656,7 @@ bool FILESYSTEM_saveTiXml2Document(const char *name, tinyxml2::XMLDocument& doc)
bool FILESYSTEM_loadTiXml2Document(const char *name, tinyxml2::XMLDocument& doc) bool FILESYSTEM_loadTiXml2Document(const char *name, tinyxml2::XMLDocument& doc)
{ {
/* XMLDocument.LoadFile doesn't account for Unicode paths, PHYSFS does */ /* XMLDocument.LoadFile doesn't account for Unicode paths, PHYSFS does */
unsigned char *mem = NULL; unsigned char *mem;
FILESYSTEM_loadFileToMemory(name, &mem, NULL, true); FILESYSTEM_loadFileToMemory(name, &mem, NULL, true);
if (mem == NULL) if (mem == NULL)
{ {

View file

@ -361,7 +361,7 @@ void Graphics::Makebfont(void)
flipbfont.push_back(TempFlipped); flipbfont.push_back(TempFlipped);
}) })
unsigned char* charmap = NULL; unsigned char* charmap;
size_t length; size_t length;
FILESYSTEM_loadAssetToMemory("graphics/font.txt", &charmap, &length, false); FILESYSTEM_loadAssetToMemory("graphics/font.txt", &charmap, &length, false);
if (charmap != NULL) if (charmap != NULL)

View file

@ -34,8 +34,8 @@ static SDL_Surface* LoadImage(const char *filename, bool noBlend = true, bool no
unsigned char *data; unsigned char *data;
unsigned int width, height; unsigned int width, height;
unsigned char *fileIn = NULL; unsigned char *fileIn;
size_t length = 0; size_t length;
FILESYSTEM_loadAssetToMemory(filename, &fileIn, &length, false); FILESYSTEM_loadAssetToMemory(filename, &fileIn, &length, false);
if (noAlpha) if (noAlpha)
{ {

View file

@ -127,8 +127,8 @@ void Screen::GetSettings(ScreenSettings* settings)
void Screen::LoadIcon(void) void Screen::LoadIcon(void)
{ {
#ifndef __APPLE__ #ifndef __APPLE__
unsigned char *fileIn = NULL; unsigned char *fileIn;
size_t length = 0; size_t length;
unsigned char *data; unsigned char *data;
unsigned int width, height; unsigned int width, height;
FILESYSTEM_loadAssetToMemory("VVVVVV.png", &fileIn, &length, false); FILESYSTEM_loadAssetToMemory("VVVVVV.png", &fileIn, &length, false);

View file

@ -30,7 +30,7 @@ MusicTrack::MusicTrack(SDL_RWops *rw)
SoundTrack::SoundTrack(const char* fileName) SoundTrack::SoundTrack(const char* fileName)
{ {
unsigned char *mem; unsigned char *mem;
size_t length = 0; size_t length;
sound = NULL; sound = NULL;

View file

@ -248,7 +248,7 @@ void editorclass::getDirectoryData(void)
} }
bool editorclass::getLevelMetaData(std::string& _path, LevelMetaData& _data ) bool editorclass::getLevelMetaData(std::string& _path, LevelMetaData& _data )
{ {
unsigned char *uMem = NULL; unsigned char *uMem;
FILESYSTEM_loadFileToMemory(_path.c_str(), &uMem, NULL, true); FILESYSTEM_loadFileToMemory(_path.c_str(), &uMem, NULL, true);
if (uMem == NULL) if (uMem == NULL)