From 0d1fbfb755aa7856a8b94c74be2e116a0ca743cb Mon Sep 17 00:00:00 2001 From: Megamouse Date: Thu, 25 Jan 2024 10:20:06 +0100 Subject: [PATCH] VFS: Fix some potential .back() segfaults --- Utilities/File.cpp | 2 +- rpcs3/Emu/vfs_config.cpp | 33 +++++++++++++++++++------- rpcs3/rpcs3qt/vfs_dialog_usb_input.cpp | 1 + 3 files changed, 26 insertions(+), 10 deletions(-) diff --git a/Utilities/File.cpp b/Utilities/File.cpp index 36e762781c..b576992163 100644 --- a/Utilities/File.cpp +++ b/Utilities/File.cpp @@ -772,7 +772,7 @@ bool fs::create_path(const std::string& path) #ifdef _WIN32 // Workaround: don't call is_dir with naked drive letter - if (parent.size() < path.size() && parent.back() != ':' && !is_dir(parent) && !create_path(parent)) + if (parent.size() < path.size() && (parent.empty() || (parent.back() != ':' && !is_dir(parent) && !create_path(parent)))) #else if (parent.size() < path.size() && !is_dir(parent) && !create_path(parent)) #endif diff --git a/rpcs3/Emu/vfs_config.cpp b/rpcs3/Emu/vfs_config.cpp index 4ff5dd6659..c650bfaeeb 100644 --- a/rpcs3/Emu/vfs_config.cpp +++ b/rpcs3/Emu/vfs_config.cpp @@ -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; } diff --git a/rpcs3/rpcs3qt/vfs_dialog_usb_input.cpp b/rpcs3/rpcs3qt/vfs_dialog_usb_input.cpp index b39a89c011..0f4970ee90 100644 --- a/rpcs3/rpcs3qt/vfs_dialog_usb_input.cpp +++ b/rpcs3/rpcs3qt/vfs_dialog_usb_input.cpp @@ -11,6 +11,7 @@ vfs_dialog_usb_input::vfs_dialog_usb_input(const QString& name, const cfg::devic : QDialog(parent), m_gui_settings(std::move(_gui_settings)), m_gui_save(gui::fs_dev_usb_list) { ensure(!!info); + ensure(!name.isEmpty()); ensure(name.back() >= '0' && name.back() <= '7'); setWindowTitle(tr("Edit %0").arg(name));