From bd0c56c6e7ad506d7123c7e3aca85bd037275a5c Mon Sep 17 00:00:00 2001 From: Vitor K Date: Wed, 25 Mar 2020 16:33:37 -0300 Subject: common: Port some changes from dolphin (#5127) * IOFile: Make the move constructor and move assignment operator noexcept Certain parts of the standard library try to determine whether or not a transfer operation should either be a copy or a move. The prevalent notion of move constructors/assignment operators is that they should not throw, they simply move an already existing resource somewhere else. This is typically done with 'std::move_if_noexcept'. Like the name says, if a type's move constructor is noexcept, then the functions retrieves an r-value reference (for move semantics), or an l-value (for copy semantics) if it is not noexcept. As IOFile deletes the copy constructor and copy assignment operators, using IOFile with certain parts of the standard library can fail in unexcepted ways (especially when used with various container implementations). This prevents that. * fix various instances of -1 being assigned to unsigned types * do not assign in conditional statements * File/IOFile: Check _tfopen_s properly * common/file_util.cpp: address review comments Co-authored-by: Lioncash Co-authored-by: Shawn Hoffman Co-authored-by: Sepalani --- src/common/file_util.cpp | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) (limited to 'src/common/file_util.cpp') diff --git a/src/common/file_util.cpp b/src/common/file_util.cpp index 41167f57a..35eee0096 100644 --- a/src/common/file_util.cpp +++ b/src/common/file_util.cpp @@ -3,6 +3,7 @@ // Refer to the license.txt file included. #include +#include #include #include #include @@ -530,11 +531,11 @@ void CopyDir(const std::string& source_path, const std::string& dest_path) { std::optional GetCurrentDir() { // Get the current working directory (getcwd uses malloc) #ifdef _WIN32 - wchar_t* dir; - if (!(dir = _wgetcwd(nullptr, 0))) { + wchar_t* dir = _wgetcwd(nullptr, 0); + if (!dir) { #else - char* dir; - if (!(dir = getcwd(nullptr, 0))) { + char* dir = getcwd(nullptr, 0); + if (!dir) { #endif LOG_ERROR(Common_Filesystem, "GetCurrentDirectory failed: {}", GetLastErrorMsg()); return {}; @@ -918,19 +919,22 @@ void IOFile::Swap(IOFile& other) noexcept { bool IOFile::Open(const std::string& filename, const char openmode[], int flags) { Close(); + bool m_good; #ifdef _WIN32 if (flags != 0) { m_file = _wfsopen(Common::UTF8ToUTF16W(filename).c_str(), Common::UTF8ToUTF16W(openmode).c_str(), flags); + m_good = m_file != nullptr; } else { - _wfopen_s(&m_file, Common::UTF8ToUTF16W(filename).c_str(), - Common::UTF8ToUTF16W(openmode).c_str()); + m_good = _wfopen_s(&m_file, Common::UTF8ToUTF16W(filename).c_str(), + Common::UTF8ToUTF16W(openmode).c_str()) == 0; } #else - m_file = fopen(filename.c_str(), openmode); + m_file = std::fopen(filename.c_str(), openmode); + m_good = m_file != nullptr; #endif - return IsOpen(); + return m_good; } bool IOFile::Close() { @@ -956,7 +960,7 @@ u64 IOFile::Tell() const { if (IsOpen()) return ftello(m_file); - return -1; + return std::numeric_limits::max(); } bool IOFile::Flush() { -- cgit v1.2.3