1
0
mirror of https://github.com/TerryCavanagh/VVVVVV.git synced 2024-06-11 15:23:38 +02: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);
}