Always check return value of PHYSFSX_getRealPath

The contents of the output buffer are undefined if PHYSFSX_getRealPath
fails, so mark the function as [[nodiscard]] and modify all callers to
check that the function succeeded.
This commit is contained in:
Kp 2022-03-19 22:55:58 +00:00
parent 3b5b69cb97
commit 9cdf9152bc
4 changed files with 14 additions and 17 deletions

View file

@ -445,6 +445,7 @@ PHYSFS_ErrorCode PHYSFSX_addRelToSearchPath(const char *relname, std::array<char
void PHYSFSX_removeRelFromSearchPath(const char *relname);
extern int PHYSFSX_fsize(const char *hogname);
extern void PHYSFSX_listSearchPathContent();
[[nodiscard]]
int PHYSFSX_getRealPath(const char *stdPath, std::array<char, PATH_MAX> &realPath);
class PHYSFS_unowned_storage_mount_deleter

View file

@ -142,8 +142,8 @@ static std::unique_ptr<FILE, FILE_deleter> open_m3u_from_disk(const char *const
std::array<char, PATH_MAX> absbuf;
return std::unique_ptr<FILE, FILE_deleter>(fopen(
// it's a child of Sharepath, build full path
(PHYSFSX_exists(cfgpath, 0)
? (PHYSFSX_getRealPath(cfgpath, absbuf), absbuf.data())
(PHYSFSX_exists(cfgpath, 0) && PHYSFSX_getRealPath(cfgpath, absbuf)
? absbuf.data()
: cfgpath), "rb")
);
}

View file

@ -255,7 +255,8 @@ static int select_file_recursive(const menu_title title, const std::array<char,
static window_event_result get_absolute_path(ntstring<PATH_MAX - 1> &full_path, const char *rel_path)
{
PHYSFSX_getRealPath(rel_path, full_path);
if (!PHYSFSX_getRealPath(rel_path, full_path))
full_path.front() = 0;
return window_event_result::close;
}
@ -2358,11 +2359,8 @@ static int select_file_recursive(const menu_title title, const std::array<char,
std::array<char, PATH_MAX> new_path;
// Check for a PhysicsFS path first, saves complication!
if (strncmp(orig_path, sep, strlen(sep)) && PHYSFSX_exists(orig_path,0))
{
PHYSFSX_getRealPath(orig_path, new_path);
if (strncmp(orig_path, sep, strlen(sep)) && PHYSFSX_exists(orig_path, 0) && PHYSFSX_getRealPath(orig_path, new_path))
orig_path = new_path.data();
}
try {
auto b = window_create<browser>(orig_path, title, ext_range, select_dir, sep, userdata);

View file

@ -473,8 +473,8 @@ int PHYSFSX_getRealPath(const char *stdPath, std::array<char, PATH_MAX> &realPat
int PHYSFSX_rename(const char *oldpath, const char *newpath)
{
std::array<char, PATH_MAX> old, n;
PHYSFSX_getRealPath(oldpath, old);
PHYSFSX_getRealPath(newpath, n);
if (!PHYSFSX_getRealPath(oldpath, old) || !PHYSFSX_getRealPath(newpath, n))
return -1;
return (rename(old.data(), n.data()) == 0);
}
@ -583,8 +583,7 @@ void PHYSFSX_addArchiveContent()
range_for (const auto i, list)
{
std::array<char, PATH_MAX> realfile;
PHYSFSX_getRealPath(i,realfile);
if (PHYSFS_mount(realfile.data(), nullptr, 0))
if (PHYSFSX_getRealPath(i, realfile) && PHYSFS_mount(realfile.data(), nullptr, 0))
{
con_printf(CON_DEBUG, "PHYSFS: Added %s to Search Path",realfile.data());
content_updated = 1;
@ -600,8 +599,7 @@ void PHYSFSX_addArchiveContent()
char demofile[PATH_MAX];
snprintf(demofile, sizeof(demofile), DEMO_DIR "%s", i);
std::array<char, PATH_MAX> realfile;
PHYSFSX_getRealPath(demofile,realfile);
if (PHYSFS_mount(realfile.data(), DEMO_DIR, 0))
if (PHYSFSX_getRealPath(demofile, realfile) && PHYSFS_mount(realfile.data(), DEMO_DIR, 0))
{
con_printf(CON_DEBUG, "PHYSFS: Added %s to " DEMO_DIR, realfile.data());
content_updated = 1;
@ -626,8 +624,8 @@ void PHYSFSX_removeArchiveContent()
range_for (const auto i, list)
{
std::array<char, PATH_MAX> realfile;
PHYSFSX_getRealPath(i, realfile);
PHYSFS_unmount(realfile.data());
if (PHYSFSX_getRealPath(i, realfile))
PHYSFS_unmount(realfile.data());
}
list.reset();
// find files in DEMO_DIR ...
@ -638,8 +636,8 @@ void PHYSFSX_removeArchiveContent()
char demofile[PATH_MAX];
snprintf(demofile, sizeof(demofile), DEMO_DIR "%s", i);
std::array<char, PATH_MAX> realfile;
PHYSFSX_getRealPath(demofile,realfile);
PHYSFS_unmount(realfile.data());
if (PHYSFSX_getRealPath(demofile, realfile))
PHYSFS_unmount(realfile.data());
}
}