diff --git a/Utilities/File.cpp b/Utilities/File.cpp index 30e6414675..d2adb22c61 100644 --- a/Utilities/File.cpp +++ b/Utilities/File.cpp @@ -398,12 +398,11 @@ namespace fs class windows_file final : public file_base { HANDLE m_handle; - atomic_t m_pos; + atomic_t m_pos {0}; public: windows_file(HANDLE handle) : m_handle(handle) - , m_pos(0) { } @@ -417,10 +416,10 @@ namespace fs stat_t get_stat() override { - FILE_BASIC_INFO basic_info; + FILE_BASIC_INFO basic_info {}; ensure(GetFileInformationByHandleEx(m_handle, FileBasicInfo, &basic_info, sizeof(FILE_BASIC_INFO))); // "file::stat" - stat_t info; + stat_t info {}; info.is_directory = (basic_info.FileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0; info.is_writable = (basic_info.FileAttributes & FILE_ATTRIBUTE_READONLY) == 0; info.size = this->size(); @@ -441,7 +440,7 @@ namespace fs bool trunc(u64 length) override { - FILE_END_OF_FILE_INFO _eof; + FILE_END_OF_FILE_INFO _eof {}; _eof.EndOfFile.QuadPart = length; if (!SetFileInformationByHandle(m_handle, FileEndOfFileInfo, &_eof, sizeof(_eof))) @@ -563,6 +562,7 @@ namespace fs u64 size() override { + // NOTE: this can fail if we access a mounted empty drive (e.g. after unmounting an iso). LARGE_INTEGER size; ensure(GetFileSizeEx(m_handle, &size)); // "file::size" @@ -579,7 +579,7 @@ namespace fs file_id id{"windows_file"}; id.data.resize(sizeof(FILE_ID_INFO)); - FILE_ID_INFO info; + FILE_ID_INFO info {}; if (!GetFileInformationByHandleEx(m_handle, FileIdInfo, &info, sizeof(info))) { @@ -625,7 +625,7 @@ namespace fs struct ::stat file_info; ensure(::fstat(m_fd, &file_info) == 0); // "file::stat" - stat_t info; + stat_t info {}; info.is_directory = S_ISDIR(file_info.st_mode); info.is_writable = file_info.st_mode & 0200; // HACK: approximation info.size = file_info.st_size; @@ -1656,6 +1656,16 @@ fs::file::file(const std::string& path, bs_t mode) return; } + // Check if the handle is actually valid. + // This can fail on empty mounted drives (e.g. with ERROR_NOT_READY or ERROR_INVALID_FUNCTION). + BY_HANDLE_FILE_INFORMATION info; + if (!GetFileInformationByHandle(handle, &info)) + { + CloseHandle(handle); + g_tls_error = to_error(GetLastError()); + return; + } + m_file = std::make_unique(handle); #else int flags = O_CLOEXEC; // Ensures all files are closed on execl for auto updater diff --git a/Utilities/File.h b/Utilities/File.h index 7e6356da7b..dd2db42a46 100644 --- a/Utilities/File.h +++ b/Utilities/File.h @@ -66,13 +66,13 @@ namespace fs // File attributes (TODO) struct stat_t { - bool is_directory; - bool is_symlink; - bool is_writable; - u64 size; - s64 atime; - s64 mtime; - s64 ctime; + bool is_directory = false; + bool is_symlink = false; + bool is_writable = false; + u64 size = 0; + s64 atime = 0; + s64 mtime = 0; + s64 ctime = 0; using enable_bitcopy = std::true_type; diff --git a/rpcs3/Loader/ISO.h b/rpcs3/Loader/ISO.h index 3af4732aad..588e4b8491 100644 --- a/rpcs3/Loader/ISO.h +++ b/rpcs3/Loader/ISO.h @@ -13,16 +13,16 @@ void unload_iso(); struct iso_extent_info { - u64 start; - u64 size; + u64 start = 0; + u64 size = 0; }; struct iso_fs_metadata { std::string name; - s64 time; - bool is_directory; - bool has_multiple_extents; + s64 time = 0; + bool is_directory = false; + bool has_multiple_extents = false; std::vector extents; u64 size() const; @@ -30,7 +30,7 @@ struct iso_fs_metadata struct iso_fs_node { - iso_fs_metadata metadata; + iso_fs_metadata metadata {}; std::vector> children; }; @@ -38,7 +38,7 @@ class iso_file : public fs::file_base { private: fs::file m_file; - iso_fs_metadata m_meta; + iso_fs_metadata m_meta {}; u64 m_pos = 0; std::pair get_extent_pos(u64 pos) const; @@ -80,7 +80,7 @@ class iso_archive { private: std::string m_path; - iso_fs_node m_root; + iso_fs_node m_root {}; fs::file m_file; public: