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

Always add null terminator when loading files

This removes the `addnull` argument from `FILESYSTEM_loadFileToMemory`
and `FILESYSTEM_loadAssetToMemory`, and makes it so a null terminator is
always appended no matter what.

This simplifies things and removes the need for callers to make the
decision about null termination and what its implications are. Then you
get cases where null termination might not happen when it should be,
such as the one df577c59ef (#947) fixed.

When FIQ added the `addnull` argument in
5862af4445 (#117), I'm guessing he did it
because he wanted to be cautious about adding the null terminator to
every file, so he only did it for XML files, which was the only case
needed at the time. But really, there's no downsides to always appending
a null terminator. In fact, it's already always done whenever the STDIN
buffer is loaded.
This commit is contained in:
Misa 2023-03-18 15:12:24 -07:00
parent fb15a0b515
commit 63bc71b796
6 changed files with 18 additions and 37 deletions

View file

@ -286,7 +286,7 @@ void customlevelclass::getDirectoryData(void)
bool customlevelclass::getLevelMetaDataAndPlaytestArgs(const std::string& _path, LevelMetaData& _data, CliPlaytestArgs* pt_args)
{
unsigned char *uMem;
FILESYSTEM_loadFileToMemory(_path.c_str(), &uMem, NULL, true);
FILESYSTEM_loadFileToMemory(_path.c_str(), &uMem, NULL);
if (uMem == NULL)
{

View file

@ -697,8 +697,7 @@ static void load_stdin(void)
bool end = ch == EOF;
if (end)
{
/* Add null terminator. There's no observable change in
* behavior if addnull is always true, but not vice versa. */
/* Always add null terminator. */
ch = '\0';
}
@ -729,8 +728,7 @@ static void load_stdin(void)
void FILESYSTEM_loadFileToMemory(
const char *name,
unsigned char **mem,
size_t *len,
bool addnull
size_t *len
) {
PHYSFS_File *handle;
PHYSFS_sint64 length;
@ -741,12 +739,6 @@ void FILESYSTEM_loadFileToMemory(
goto fail;
}
if (len == NULL && !addnull)
{
vlog_warn("%s is loaded with len == NULL && !addnull", name);
SDL_assert(0 && "Are you sure you don't want a null terminator to be added to these loaded file contents?");
}
/* FIXME: Dumb hack to use `special/stdin.vvvvvv` here...
* This is also checked elsewhere... grep for `special/stdin`! */
if (SDL_strcmp(name, "levels/special/stdin.vvvvvv") == 0)
@ -791,23 +783,14 @@ void FILESYSTEM_loadFileToMemory(
}
*len = length;
}
if (addnull)
*mem = (unsigned char *) SDL_malloc(length + 1);
if (*mem == NULL)
{
*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);
}
VVV_exit(1);
}
(*mem)[length] = 0;
success = PHYSFS_readBytes(handle, *mem, length);
if (success == -1)
{
@ -830,14 +813,13 @@ fail:
void FILESYSTEM_loadAssetToMemory(
const char* name,
unsigned char** mem,
size_t* len,
const bool addnull
size_t* len
) {
char path[MAX_PATH];
getMountedPath(path, sizeof(path), name);
FILESYSTEM_loadFileToMemory(path, mem, len, addnull);
FILESYSTEM_loadFileToMemory(path, mem, len);
}
bool FILESYSTEM_loadBinaryBlob(binaryBlob* blob, const char* filename)
@ -980,7 +962,7 @@ bool FILESYSTEM_loadTiXml2Document(const char *name, tinyxml2::XMLDocument& doc)
{
/* XMLDocument.LoadFile doesn't account for Unicode paths, PHYSFS does */
unsigned char *mem;
FILESYSTEM_loadFileToMemory(name, &mem, NULL, true);
FILESYSTEM_loadFileToMemory(name, &mem, NULL);
if (mem == NULL)
{
return false;
@ -994,7 +976,7 @@ bool FILESYSTEM_loadAssetTiXml2Document(const char *name, tinyxml2::XMLDocument&
{
/* Same as FILESYSTEM_loadTiXml2Document except for possible custom assets */
unsigned char *mem;
FILESYSTEM_loadAssetToMemory(name, &mem, NULL, true);
FILESYSTEM_loadAssetToMemory(name, &mem, NULL);
if (mem == NULL)
{
return false;

View file

@ -31,12 +31,11 @@ bool FILESYSTEM_isAssetMounted(const char* filename);
bool FILESYSTEM_areAssetsInSameRealDir(const char* filenameA, const char* filenameB);
void FILESYSTEM_loadFileToMemory(const char *name, unsigned char **mem,
size_t *len, bool addnull);
size_t *len);
void FILESYSTEM_loadAssetToMemory(
const char* name,
unsigned char** mem,
size_t* len,
const bool addnull
size_t* len
);
bool FILESYSTEM_loadBinaryBlob(binaryBlob* blob, const char* filename);

View file

@ -349,7 +349,7 @@ static uint8_t load_font(FontContainer* container, const char* name)
{
/* The .txt can contain null bytes, but it's still null-terminated - it protects
* against incomplete sequences getting the UTF-8 decoder to read out of bounds. */
FILESYSTEM_loadAssetToMemory(name_txt, &charmap, &length, true);
FILESYSTEM_loadAssetToMemory(name_txt, &charmap, &length);
}
if (charmap != NULL)
{

View file

@ -29,7 +29,7 @@ static SDL_Surface* LoadImageRaw(const char* filename, unsigned char** data)
unsigned char* fileIn;
size_t length;
FILESYSTEM_loadAssetToMemory(filename, &fileIn, &length, false);
FILESYSTEM_loadAssetToMemory(filename, &fileIn, &length);
if (fileIn == NULL)
{
SDL_assert(0 && "Image file missing!");

View file

@ -100,7 +100,7 @@ public:
SDL_AudioSpec spec;
SDL_RWops *fileIn;
SDL_zerop(this);
FILESYSTEM_loadAssetToMemory(fileName, &mem, &length, false);
FILESYSTEM_loadAssetToMemory(fileName, &mem, &length);
if (mem == NULL)
{
vlog_error("Unable to load WAV file %s", fileName);