From 635de4fa93e241abbf05f0ceaba3516bd789d991 Mon Sep 17 00:00:00 2001 From: Ryan Burns <52847440+r-burns@users.noreply.github.com> Date: Fri, 2 Dec 2022 01:46:38 -0800 Subject: [PATCH] Handle GetCurrentDirectory required buffer size return value (#27) When the output buffer size is too small, GetCurrentDirectory does nothing and simply returns the larger required size. https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-getcurrentdirectory#return-value Needed to run Code Warrior 4 mwcc.exe with no arguments, displaying usage/help message. (Still unable to compile/preprocess with CW4 mwcc.exe) --- dll/kernel32.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/dll/kernel32.cpp b/dll/kernel32.cpp index dae41df..8d3f59d 100644 --- a/dll/kernel32.cpp +++ b/dll/kernel32.cpp @@ -932,7 +932,11 @@ namespace kernel32 { std::filesystem::path cwd = std::filesystem::current_path(); std::string path = files::pathToWindows(cwd); - assert(path.size() < uSize); + // If the buffer is too small, return the required buffer size. + // (Add 1 to include the NUL terminator) + if (path.size() + 1 > uSize) { + return path.size() + 1; + } strcpy(lpBuffer, path.c_str()); return path.size();