From e9ddf91157a8903128d3030acd9985eb3c98a755 Mon Sep 17 00:00:00 2001 From: Cameron Gutman Date: Sat, 16 Apr 2022 12:47:33 -0500 Subject: [PATCH] Don't crash if the log file can't be opened --- GS-IPv6-Forwarder | 2 +- miss/miss.cpp | 24 +++++++++++++++++++----- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/GS-IPv6-Forwarder b/GS-IPv6-Forwarder index 2115224..16ddcb8 160000 --- a/GS-IPv6-Forwarder +++ b/GS-IPv6-Forwarder @@ -1 +1 @@ -Subproject commit 21152240cfbf8c6a35c665575b8b25de7ed36b40 +Subproject commit 16ddcb82d94dd99a8bee8887434acd528411773c diff --git a/miss/miss.cpp b/miss/miss.cpp index 0480240..5014268 100644 --- a/miss/miss.cpp +++ b/miss/miss.cpp @@ -11,6 +11,8 @@ #include #include #include +#include +#include #include "relay.h" #include "..\version.h" @@ -834,15 +836,27 @@ void ResetLogFile(bool standaloneExe) ExpandEnvironmentStringsA("%ProgramData%\\MISS\\miss-old.log", oldLogFilePath, sizeof(oldLogFilePath)); ExpandEnvironmentStringsA("%ProgramData%\\MISS\\miss-current.log", currentLogFilePath, sizeof(currentLogFilePath)); - // Close the existing stdout handle. This is important because otherwise - // it may still be open as stdout when we try to MoveFileEx below. - fclose(stdout); - // Rotate the current to the old log file MoveFileExA(currentLogFilePath, oldLogFilePath, MOVEFILE_REPLACE_EXISTING); + // Open the new log file + HANDLE newLogHandle = CreateFileA(currentLogFilePath, + GENERIC_WRITE, + FILE_SHARE_READ | FILE_SHARE_DELETE, + nullptr, + CREATE_ALWAYS, + FILE_ATTRIBUTE_NORMAL, + nullptr); + if (newLogHandle == INVALID_HANDLE_VALUE) { + return; + } + + // Convert the HANDLE to an fd (transfers ownership too) + int fd = _open_osfhandle((intptr_t)newLogHandle, _O_WRONLY); + // Redirect stdout to this new file - freopen(currentLogFilePath, "w", stdout); + _dup2(fd, _fileno(stdout)); + _close(fd); } // Print a log header