From 5af570e75be1420dd4a58117f61ccebd4286acb7 Mon Sep 17 00:00:00 2001 From: Misa Date: Thu, 4 Mar 2021 14:37:21 -0800 Subject: [PATCH] Set length to 0 if PHYSFS_fileLength() is negative PHYSFS_fileLength() returns -1 if the file size can't be determined. I'm going to set it to 0 instead, because it seems like that's more well-behaved with consumers. Take lodepng_decode24() or lodepng_decode32(), for example - from a quick glance at the source, it only takes in a size_t (an unsigned integer) for the filesize, and one of the first things it does is malloc with the given filesize. If the -1 turns into SIZE_MAX and LodePNG attempts to allocate that many bytes... well, I don't know of any systems that have 18 exabytes of memory. So that seems pretty bad. --- desktop_version/src/FileSystemUtils.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/desktop_version/src/FileSystemUtils.cpp b/desktop_version/src/FileSystemUtils.cpp index dd0724c8..cda6924c 100644 --- a/desktop_version/src/FileSystemUtils.cpp +++ b/desktop_version/src/FileSystemUtils.cpp @@ -363,6 +363,10 @@ void FILESYSTEM_loadFileToMemory( PHYSFS_sint64 length = PHYSFS_fileLength(handle); if (len != NULL) { + if (length < 0) + { + length = 0; + } *len = length; } if (addnull)