From 349a2e92359b7055cd8f0fd46f6a26692748b536 Mon Sep 17 00:00:00 2001 From: Cacodemon345 Date: Fri, 12 Feb 2021 11:52:26 +0600 Subject: [PATCH] Fix crash when trying to open unreadable IWAD files --- src/common/utility/cmdlib.cpp | 17 +++++++++++++++++ src/common/utility/cmdlib.h | 1 + src/d_iwad.cpp | 4 ++-- 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/common/utility/cmdlib.cpp b/src/common/utility/cmdlib.cpp index 8786a1c70..c162dd0d3 100644 --- a/src/common/utility/cmdlib.cpp +++ b/src/common/utility/cmdlib.cpp @@ -165,6 +165,23 @@ bool FileExists (const char *filename) return res && !isdir; } +//========================================================================== +// +// FileReadable +// +// Returns true if the file can be read. +// +//========================================================================== + +bool FileReadable(const char *filename) +{ +#ifndef _WIN32 + return access (filename, R_OK) == 0; +#else + return _access (filename, 4) == 0; +#endif +} + //========================================================================== // // DirExists diff --git a/src/common/utility/cmdlib.h b/src/common/utility/cmdlib.h index 4c4468c98..3792030ae 100644 --- a/src/common/utility/cmdlib.h +++ b/src/common/utility/cmdlib.h @@ -33,6 +33,7 @@ char(&_ArraySizeHelper(T(&array)[N]))[N]; #define myoffsetof(type,identifier) ((size_t)&((type *)alignof(type))->identifier - alignof(type)) bool FileExists (const char *filename); +bool FileReadable (const char *filename); bool DirExists(const char *filename); bool DirEntryExists (const char *pathname, bool *isdir = nullptr); bool GetFileInfo(const char* pathname, size_t* size, time_t* time); diff --git a/src/d_iwad.cpp b/src/d_iwad.cpp index 289b67671..6abc0dda6 100644 --- a/src/d_iwad.cpp +++ b/src/d_iwad.cpp @@ -577,10 +577,10 @@ int FIWadManager::IdentifyVersion (TArray &wadfiles, const char *iwad, // -iwad not found or not specified. Revert back to standard behavior. if (mFoundWads.Size() == numFoundWads) iwadparm = nullptr; - // Check for symbolic links leading to non-existent files. + // Check for symbolic links leading to non-existent files and for files that are unreadable. for (unsigned int i = 0; i < mFoundWads.Size(); i++) { - if (!FileExists(mFoundWads[i].mFullPath)) mFoundWads.Delete(i); + if (!FileExists(mFoundWads[i].mFullPath) || !FileReadable(mFoundWads[i].mFullPath)) mFoundWads.Delete(i); } // Now check if what got collected actually is an IWAD.