1
0
Fork 0
mirror of https://github.com/TerryCavanagh/VVVVVV.git synced 2024-10-31 16:29:41 +01:00

Handle errors from PHYSFS_readBytes()

This fixes a bug where levels in the levels list duplicate if there's an
invalid file (such as a folder) in the levels directory.

It looks like it happens because we don't free the memory if
PHYSFS_readBytes() encounters an error, even though we should. Then we
get into Undefined Behavior territory and end up reusing memory, and
here it just happens that previously, parsing the entire XML document
for each level file was enough to make the loaded file pointer point to
garbage that would fail the metadata check, but if we optimize it so we
don't parse the entire XML document, it starts reusing memory instead.
This commit is contained in:
Misa 2020-04-17 15:33:37 -07:00 committed by Ethan Lee
parent 9c4c76f609
commit ec3f937f93

View file

@ -189,7 +189,11 @@ void FILESYSTEM_loadFileToMemory(const char *name, unsigned char **mem,
{
*mem = (unsigned char*) malloc(length);
}
PHYSFS_readBytes(handle, *mem, length);
int success = PHYSFS_readBytes(handle, *mem, length);
if (success == -1)
{
FILESYSTEM_freeMemory(mem);
}
PHYSFS_close(handle);
}