VFS: Fix some potential .back() segfaults

This commit is contained in:
Megamouse 2024-01-25 10:20:06 +01:00
parent 10c52cf569
commit 0d1fbfb755
3 changed files with 26 additions and 10 deletions

View file

@ -14,6 +14,20 @@ std::string cfg_vfs::get(const cfg::string& _cfg, std::string_view emu_dir) cons
std::string cfg_vfs::get(const std::string& _cfg, const std::string& def, std::string_view emu_dir) const
{
std::string path = _cfg;
// Fallback
if (path.empty())
{
if (def.empty())
{
vfs_log.notice("VFS config called with empty path and empty default");
return {};
}
path = def;
}
std::string _emu_dir; // Storage only
if (emu_dir.empty())
@ -34,18 +48,14 @@ std::string cfg_vfs::get(const std::string& _cfg, const std::string& def, std::s
emu_dir = _emu_dir;
}
std::string path = _cfg;
if (path.empty())
{
// Fallback
path = def;
}
path = fmt::replace_all(path, "$(EmulatorDir)", emu_dir);
// Check if path does not end with a delimiter
if (path.back() != fs::delim[0] && path.back() != fs::delim[1])
if (path.empty())
{
vfs_log.error("VFS config path empty (_cfg='%s', def='%s', emu_dir='%s')", _cfg, def, emu_dir);
}
else if (path.back() != fs::delim[0] && path.back() != fs::delim[1])
{
path += '/';
}
@ -74,6 +84,11 @@ cfg::device_info cfg_vfs::get_device_info(const cfg::device_entry& _cfg, std::st
def_path = def_it->second.path;
}
if (info.path.empty() && def_path.empty())
{
return info;
}
info.path = get(info.path, def_path, emu_dir);
return info;
}