Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Additional null checks & file failure handling #4063

Merged
merged 2 commits into from
Sep 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/updatemanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -726,16 +726,23 @@ static bool cacheInfoIsUsable(CachePaths& paths)
try {
// Open the file + read the data
PHYSFS_file *fileHandle = PHYSFS_openRead(paths.cache_info_path);
if (fileHandle == nullptr)
{
// Unable to open file
throw std::runtime_error("Failed opening file");
}
PHYSFS_sint64 filesize = PHYSFS_fileLength(fileHandle);
if (filesize <= 0)
{
// Invalid file size
PHYSFS_close(fileHandle);
throw std::runtime_error("Invalid filesize");
}
std::vector<char> fileData(static_cast<size_t>(filesize + 1), '\0');
if (WZ_PHYSFS_readBytes(fileHandle, fileData.data(), static_cast<PHYSFS_uint32>(filesize)) != filesize)
{
// Read failed
PHYSFS_close(fileHandle);
throw std::runtime_error("Read failed");
}
PHYSFS_close(fileHandle);
Expand Down Expand Up @@ -777,16 +784,23 @@ static void initProcessData(const std::vector<std::string> &updateDataUrls, Proc
try {
// Open the file + read the data
PHYSFS_file *fileHandle = PHYSFS_openRead(outputPaths.cache_data_path);
if (fileHandle == nullptr)
{
// Unable to open file
throw std::runtime_error("Failed opening file");
}
PHYSFS_sint64 filesize = PHYSFS_fileLength(fileHandle);
if (filesize < 0 || filesize > WZ_UPDATES_JSON_MAX_SIZE)
{
// Invalid file size
PHYSFS_close(fileHandle);
throw std::runtime_error("Invalid filesize");
}
std::vector<char> fileData(static_cast<size_t>(filesize + 1), '\0');
if (WZ_PHYSFS_readBytes(fileHandle, fileData.data(), static_cast<PHYSFS_uint32>(filesize)) != filesize)
{
// Read failed
PHYSFS_close(fileHandle);
throw std::runtime_error("Read failed");
}
PHYSFS_close(fileHandle);
Expand Down
Loading