Skip to content

Commit

Permalink
Merge pull request #24 from isanae/null-pointer-in-move
Browse files Browse the repository at this point in the history
(WIP) Handle null pointer in MoveFileExA() and MoveFileWithProgressA()
  • Loading branch information
LostDragonist authored Oct 20, 2019
2 parents 496d2f8 + fdb8fb6 commit 4c50507
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
2 changes: 1 addition & 1 deletion include/usvfs_version.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#define USVFS_VERSION_MAJOR 0
#define USVFS_VERSION_MINOR 4
#define USVFS_VERSION_BUILD 4
#define USVFS_VERSION_REVISION 4
#define USVFS_VERSION_REVISION 5

#define USVFS_BUILD_STRING ""
#define USVFS_BUILD_WSTRING L""
Expand Down
30 changes: 25 additions & 5 deletions src/usvfs_dll/hooks/kernel32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -675,11 +675,21 @@ BOOL WINAPI usvfs::hook_MoveFileExA(LPCSTR lpExistingFileName,
HOOK_END
HOOK_START

const auto& existingFileName = ush::string_cast<std::wstring>(lpExistingFileName);
const auto& newFileName = ush::string_cast<std::wstring>(lpNewFileName);
const std::wstring existingFileName = ush::string_cast<std::wstring>(lpExistingFileName);

// careful: lpNewFileName can be null if dwFlags is
// MOVEFILE_DELAY_UNTIL_REBOOT, so don't blindly cast the string and make sure
// the null pointer is forwarded correctly
std::wstring newFileNameWstring;
const wchar_t* newFileName = nullptr;

if (lpNewFileName) {
newFileNameWstring = ush::string_cast<std::wstring>(lpNewFileName);
newFileName = newFileNameWstring.c_str();
}

PRE_REALCALL
res = MoveFileExW(existingFileName.c_str(), newFileName.c_str(), dwFlags);
res = MoveFileExW(existingFileName.c_str(), newFileName, dwFlags);
POST_REALCALL

HOOK_END
Expand Down Expand Up @@ -792,10 +802,20 @@ BOOL WINAPI usvfs::hook_MoveFileWithProgressA(LPCSTR lpExistingFileName, LPCSTR
HOOK_START

const auto& existingFileName = ush::string_cast<std::wstring>(lpExistingFileName);
const auto& newFileName = ush::string_cast<std::wstring>(lpNewFileName);

// careful: lpNewFileName can be null if dwFlags is
// MOVEFILE_DELAY_UNTIL_REBOOT, so don't blindly cast the string and make sure
// the null pointer is forwarded correctly
std::wstring newFileNameWstring;
const wchar_t* newFileName = nullptr;

if (lpNewFileName) {
newFileNameWstring = ush::string_cast<std::wstring>(lpNewFileName);
newFileName = newFileNameWstring.c_str();
}

PRE_REALCALL
res = MoveFileWithProgressW(existingFileName.c_str(), newFileName.c_str(), lpProgressRoutine, lpData, dwFlags);
res = MoveFileWithProgressW(existingFileName.c_str(), newFileName, lpProgressRoutine, lpData, dwFlags);
POST_REALCALL

HOOK_END
Expand Down

0 comments on commit 4c50507

Please sign in to comment.