diff options
| author | Lioncash <mathew1800@gmail.com> | 2019-05-23 14:24:11 -0400 |
|---|---|---|
| committer | Lioncash <mathew1800@gmail.com> | 2019-05-23 14:24:13 -0400 |
| commit | 40b76cdcd269957ac67b9f47bad460e218da29f8 (patch) | |
| tree | f27f82a7cfedb748013fa0d6f236f8dd3583d7e2 /src/common/file_util.cpp | |
| parent | 997f43bdf80d4d7079324e45981c51ec011a0bc4 (diff) | |
common/file_util: Make GetCurrentDir() return a std::optional
nullptr was being returned in the error case, which, at a glance may
seem perfectly OK... until you realize that std::string has the
invariant that it may not be constructed from a null pointer. This
means that if this error case was ever hit, then the application would
most likely crash from a thrown exception in std::string's constructor.
Instead, we can change the function to return an optional value,
indicating if a failure occurred.
Diffstat (limited to 'src/common/file_util.cpp')
| -rw-r--r-- | src/common/file_util.cpp | 4 |
1 files changed, 2 insertions, 2 deletions
diff --git a/src/common/file_util.cpp b/src/common/file_util.cpp index 7403108078..d8812837ea 100644 --- a/src/common/file_util.cpp +++ b/src/common/file_util.cpp @@ -525,7 +525,7 @@ void CopyDir(const std::string& source_path, const std::string& dest_path) { #endif } -std::string GetCurrentDir() { +std::optional<std::string> GetCurrentDir() { // Get the current working directory (getcwd uses malloc) #ifdef _WIN32 wchar_t* dir; @@ -535,7 +535,7 @@ std::string GetCurrentDir() { if (!(dir = getcwd(nullptr, 0))) { #endif LOG_ERROR(Common_Filesystem, "GetCurrentDirectory failed: {}", GetLastErrorMsg()); - return nullptr; + return {}; } #ifdef _WIN32 std::string strDir = Common::UTF16ToUTF8(dir); |
