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 <mathew1800@gmail.com>
Co-authored-by: Shawn Hoffman <godisgovernment@gmail.com>
Co-authored-by: Sepalani <sepalani@hotmail.fr>
This commit is contained in:
Vitor K 2020-03-25 16:33:37 -03:00 committed by GitHub
parent 25ebf03c35
commit 480d3aaf19
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 21 deletions

View file

@ -3,6 +3,7 @@
// Refer to the license.txt file included. // Refer to the license.txt file included.
#include <array> #include <array>
#include <limits>
#include <memory> #include <memory>
#include <sstream> #include <sstream>
#include <unordered_map> #include <unordered_map>
@ -541,11 +542,11 @@ void CopyDir(const std::string& source_path, const std::string& dest_path) {
std::optional<std::string> GetCurrentDir() { std::optional<std::string> GetCurrentDir() {
// Get the current working directory (getcwd uses malloc) // Get the current working directory (getcwd uses malloc)
#ifdef _WIN32 #ifdef _WIN32
wchar_t* dir; wchar_t* dir = _wgetcwd(nullptr, 0);
if (!(dir = _wgetcwd(nullptr, 0))) { if (!dir) {
#else #else
char* dir; char* dir = getcwd(nullptr, 0);
if (!(dir = getcwd(nullptr, 0))) { if (!dir) {
#endif #endif
LOG_ERROR(Common_Filesystem, "GetCurrentDirectory failed: {}", GetLastErrorMsg()); LOG_ERROR(Common_Filesystem, "GetCurrentDirectory failed: {}", GetLastErrorMsg());
return {}; return {};
@ -889,16 +890,16 @@ IOFile::~IOFile() {
Close(); Close();
} }
IOFile::IOFile(IOFile&& other) { IOFile::IOFile(IOFile&& other) noexcept {
Swap(other); Swap(other);
} }
IOFile& IOFile::operator=(IOFile&& other) { IOFile& IOFile::operator=(IOFile&& other) noexcept {
Swap(other); Swap(other);
return *this; return *this;
} }
void IOFile::Swap(IOFile& other) { void IOFile::Swap(IOFile& other) noexcept {
std::swap(m_file, other.m_file); std::swap(m_file, other.m_file);
std::swap(m_good, other.m_good); std::swap(m_good, other.m_good);
} }
@ -909,15 +910,16 @@ bool IOFile::Open(const std::string& filename, const char openmode[], int flags)
if (flags != 0) { if (flags != 0) {
m_file = _wfsopen(Common::UTF8ToUTF16W(filename).c_str(), m_file = _wfsopen(Common::UTF8ToUTF16W(filename).c_str(),
Common::UTF8ToUTF16W(openmode).c_str(), flags); Common::UTF8ToUTF16W(openmode).c_str(), flags);
m_good = m_file != nullptr;
} else { } else {
_wfopen_s(&m_file, Common::UTF8ToUTF16W(filename).c_str(), m_good = _wfopen_s(&m_file, Common::UTF8ToUTF16W(filename).c_str(),
Common::UTF8ToUTF16W(openmode).c_str()); Common::UTF8ToUTF16W(openmode).c_str()) == 0;
} }
#else #else
m_file = fopen(filename.c_str(), openmode); m_file = std::fopen(filename.c_str(), openmode);
m_good = m_file != nullptr;
#endif #endif
m_good = IsOpen();
return m_good; return m_good;
} }
@ -947,7 +949,7 @@ u64 IOFile::Tell() const {
if (IsOpen()) if (IsOpen())
return ftello(m_file); return ftello(m_file);
return -1; return std::numeric_limits<u64>::max();
} }
bool IOFile::Flush() { bool IOFile::Flush() {

View file

@ -216,10 +216,10 @@ public:
~IOFile(); ~IOFile();
IOFile(IOFile&& other); IOFile(IOFile&& other) noexcept;
IOFile& operator=(IOFile&& other); IOFile& operator=(IOFile&& other) noexcept;
void Swap(IOFile& other); void Swap(IOFile& other) noexcept;
bool Open(const std::string& filename, const char openmode[], int flags = 0); bool Open(const std::string& filename, const char openmode[], int flags = 0);
bool Close(); bool Close();

View file

@ -28,11 +28,8 @@ namespace Common {
#ifdef _MSC_VER #ifdef _MSC_VER
// Sets the debugger-visible name of the current thread. // Sets the debugger-visible name of the current thread.
// Uses undocumented (actually, it is now documented) trick. // Uses trick documented in:
// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vsdebug/html/vxtsksettingthreadname.asp // https://docs.microsoft.com/en-us/visualstudio/debugger/how-to-set-a-thread-name-in-native-code
// This is implemented much nicer in upcoming msvc++, see:
// http://msdn.microsoft.com/en-us/library/xcb2z8hs(VS.100).aspx
void SetCurrentThreadName(const char* name) { void SetCurrentThreadName(const char* name) {
static const DWORD MS_VC_EXCEPTION = 0x406D1388; static const DWORD MS_VC_EXCEPTION = 0x406D1388;
@ -47,7 +44,7 @@ void SetCurrentThreadName(const char* name) {
info.dwType = 0x1000; info.dwType = 0x1000;
info.szName = name; info.szName = name;
info.dwThreadID = -1; // dwThreadID; info.dwThreadID = static_cast<DWORD>(-1);
info.dwFlags = 0; info.dwFlags = 0;
__try { __try {