mirror of
https://github.com/TerryCavanagh/VVVVVV.git
synced 2024-12-23 01:59: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 onedf577c59ef
(#947) fixed. When FIQ added the `addnull` argument in5862af4445
(#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:
parent
fb15a0b515
commit
63bc71b796
6 changed files with 18 additions and 37 deletions
|
@ -286,7 +286,7 @@ void customlevelclass::getDirectoryData(void)
|
||||||
bool customlevelclass::getLevelMetaDataAndPlaytestArgs(const std::string& _path, LevelMetaData& _data, CliPlaytestArgs* pt_args)
|
bool customlevelclass::getLevelMetaDataAndPlaytestArgs(const std::string& _path, LevelMetaData& _data, CliPlaytestArgs* pt_args)
|
||||||
{
|
{
|
||||||
unsigned char *uMem;
|
unsigned char *uMem;
|
||||||
FILESYSTEM_loadFileToMemory(_path.c_str(), &uMem, NULL, true);
|
FILESYSTEM_loadFileToMemory(_path.c_str(), &uMem, NULL);
|
||||||
|
|
||||||
if (uMem == NULL)
|
if (uMem == NULL)
|
||||||
{
|
{
|
||||||
|
|
|
@ -697,8 +697,7 @@ static void load_stdin(void)
|
||||||
bool end = ch == EOF;
|
bool end = ch == EOF;
|
||||||
if (end)
|
if (end)
|
||||||
{
|
{
|
||||||
/* Add null terminator. There's no observable change in
|
/* Always add null terminator. */
|
||||||
* behavior if addnull is always true, but not vice versa. */
|
|
||||||
ch = '\0';
|
ch = '\0';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -729,8 +728,7 @@ static void load_stdin(void)
|
||||||
void FILESYSTEM_loadFileToMemory(
|
void FILESYSTEM_loadFileToMemory(
|
||||||
const char *name,
|
const char *name,
|
||||||
unsigned char **mem,
|
unsigned char **mem,
|
||||||
size_t *len,
|
size_t *len
|
||||||
bool addnull
|
|
||||||
) {
|
) {
|
||||||
PHYSFS_File *handle;
|
PHYSFS_File *handle;
|
||||||
PHYSFS_sint64 length;
|
PHYSFS_sint64 length;
|
||||||
|
@ -741,12 +739,6 @@ void FILESYSTEM_loadFileToMemory(
|
||||||
goto fail;
|
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...
|
/* FIXME: Dumb hack to use `special/stdin.vvvvvv` here...
|
||||||
* This is also checked elsewhere... grep for `special/stdin`! */
|
* This is also checked elsewhere... grep for `special/stdin`! */
|
||||||
if (SDL_strcmp(name, "levels/special/stdin.vvvvvv") == 0)
|
if (SDL_strcmp(name, "levels/special/stdin.vvvvvv") == 0)
|
||||||
|
@ -791,23 +783,14 @@ void FILESYSTEM_loadFileToMemory(
|
||||||
}
|
}
|
||||||
*len = length;
|
*len = length;
|
||||||
}
|
}
|
||||||
if (addnull)
|
|
||||||
{
|
|
||||||
*mem = (unsigned char *) SDL_malloc(length + 1);
|
*mem = (unsigned char *) SDL_malloc(length + 1);
|
||||||
if (*mem == NULL)
|
if (*mem == NULL)
|
||||||
{
|
{
|
||||||
VVV_exit(1);
|
VVV_exit(1);
|
||||||
}
|
}
|
||||||
(*mem)[length] = 0;
|
(*mem)[length] = 0;
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
*mem = (unsigned char*) SDL_malloc(length);
|
|
||||||
if (*mem == NULL)
|
|
||||||
{
|
|
||||||
VVV_exit(1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
success = PHYSFS_readBytes(handle, *mem, length);
|
success = PHYSFS_readBytes(handle, *mem, length);
|
||||||
if (success == -1)
|
if (success == -1)
|
||||||
{
|
{
|
||||||
|
@ -830,14 +813,13 @@ fail:
|
||||||
void FILESYSTEM_loadAssetToMemory(
|
void FILESYSTEM_loadAssetToMemory(
|
||||||
const char* name,
|
const char* name,
|
||||||
unsigned char** mem,
|
unsigned char** mem,
|
||||||
size_t* len,
|
size_t* len
|
||||||
const bool addnull
|
|
||||||
) {
|
) {
|
||||||
char path[MAX_PATH];
|
char path[MAX_PATH];
|
||||||
|
|
||||||
getMountedPath(path, sizeof(path), name);
|
getMountedPath(path, sizeof(path), name);
|
||||||
|
|
||||||
FILESYSTEM_loadFileToMemory(path, mem, len, addnull);
|
FILESYSTEM_loadFileToMemory(path, mem, len);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool FILESYSTEM_loadBinaryBlob(binaryBlob* blob, const char* filename)
|
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 */
|
/* XMLDocument.LoadFile doesn't account for Unicode paths, PHYSFS does */
|
||||||
unsigned char *mem;
|
unsigned char *mem;
|
||||||
FILESYSTEM_loadFileToMemory(name, &mem, NULL, true);
|
FILESYSTEM_loadFileToMemory(name, &mem, NULL);
|
||||||
if (mem == NULL)
|
if (mem == NULL)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
|
@ -994,7 +976,7 @@ bool FILESYSTEM_loadAssetTiXml2Document(const char *name, tinyxml2::XMLDocument&
|
||||||
{
|
{
|
||||||
/* Same as FILESYSTEM_loadTiXml2Document except for possible custom assets */
|
/* Same as FILESYSTEM_loadTiXml2Document except for possible custom assets */
|
||||||
unsigned char *mem;
|
unsigned char *mem;
|
||||||
FILESYSTEM_loadAssetToMemory(name, &mem, NULL, true);
|
FILESYSTEM_loadAssetToMemory(name, &mem, NULL);
|
||||||
if (mem == NULL)
|
if (mem == NULL)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
|
|
|
@ -31,12 +31,11 @@ bool FILESYSTEM_isAssetMounted(const char* filename);
|
||||||
bool FILESYSTEM_areAssetsInSameRealDir(const char* filenameA, const char* filenameB);
|
bool FILESYSTEM_areAssetsInSameRealDir(const char* filenameA, const char* filenameB);
|
||||||
|
|
||||||
void FILESYSTEM_loadFileToMemory(const char *name, unsigned char **mem,
|
void FILESYSTEM_loadFileToMemory(const char *name, unsigned char **mem,
|
||||||
size_t *len, bool addnull);
|
size_t *len);
|
||||||
void FILESYSTEM_loadAssetToMemory(
|
void FILESYSTEM_loadAssetToMemory(
|
||||||
const char* name,
|
const char* name,
|
||||||
unsigned char** mem,
|
unsigned char** mem,
|
||||||
size_t* len,
|
size_t* len
|
||||||
const bool addnull
|
|
||||||
);
|
);
|
||||||
|
|
||||||
bool FILESYSTEM_loadBinaryBlob(binaryBlob* blob, const char* filename);
|
bool FILESYSTEM_loadBinaryBlob(binaryBlob* blob, const char* filename);
|
||||||
|
|
|
@ -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
|
/* 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. */
|
* 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)
|
if (charmap != NULL)
|
||||||
{
|
{
|
||||||
|
|
|
@ -29,7 +29,7 @@ static SDL_Surface* LoadImageRaw(const char* filename, unsigned char** data)
|
||||||
|
|
||||||
unsigned char* fileIn;
|
unsigned char* fileIn;
|
||||||
size_t length;
|
size_t length;
|
||||||
FILESYSTEM_loadAssetToMemory(filename, &fileIn, &length, false);
|
FILESYSTEM_loadAssetToMemory(filename, &fileIn, &length);
|
||||||
if (fileIn == NULL)
|
if (fileIn == NULL)
|
||||||
{
|
{
|
||||||
SDL_assert(0 && "Image file missing!");
|
SDL_assert(0 && "Image file missing!");
|
||||||
|
|
|
@ -100,7 +100,7 @@ public:
|
||||||
SDL_AudioSpec spec;
|
SDL_AudioSpec spec;
|
||||||
SDL_RWops *fileIn;
|
SDL_RWops *fileIn;
|
||||||
SDL_zerop(this);
|
SDL_zerop(this);
|
||||||
FILESYSTEM_loadAssetToMemory(fileName, &mem, &length, false);
|
FILESYSTEM_loadAssetToMemory(fileName, &mem, &length);
|
||||||
if (mem == NULL)
|
if (mem == NULL)
|
||||||
{
|
{
|
||||||
vlog_error("Unable to load WAV file %s", fileName);
|
vlog_error("Unable to load WAV file %s", fileName);
|
||||||
|
|
Loading…
Reference in a new issue