Skip to content

Commit 5c4c619

Browse files
Fix incorrect usage of GetModuleFileNameW win api (#2670)
* handle win path api call correctly
1 parent 0282d52 commit 5c4c619

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

src/aws-cpp-sdk-core/source/platform/windows/FileSystem.cpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <aws/core/platform/Environment.h>
88
#include <aws/core/utils/logging/LogMacros.h>
99
#include <aws/core/utils/StringUtils.h>
10+
#include <aws/core/utils/memory/stl/AWSVector.h>
1011
#include <cassert>
1112
#include <iostream>
1213
#include <Userenv.h>
@@ -180,14 +181,20 @@ Aws::String GetHomeDirectory()
180181

181182
Aws::String GetExecutableDirectory()
182183
{
183-
static const unsigned long long bufferSize = 256;
184-
WCHAR buffer[bufferSize];
184+
Aws::Vector<wchar_t> buffer(MAX_PATH + 1, NULL);
185+
DWORD written = GetModuleFileNameW(nullptr, buffer.data(), static_cast<DWORD>(buffer.size()));
185186

186-
memset(buffer, 0, sizeof(buffer));
187+
if (ERROR_INSUFFICIENT_BUFFER == GetLastError())
188+
{
189+
/* Max unicode path size is 2^15 + 1 additional byte for null terminator. */
190+
const DWORD unicode_max_path = 0x8000 + 1;
191+
buffer.resize(unicode_max_path, NULL);
192+
written = GetModuleFileNameW(nullptr, buffer.data(), static_cast<DWORD>(buffer.size()));
193+
}
187194

188-
if (GetModuleFileNameW(nullptr, buffer, static_cast<DWORD>(sizeof(buffer))))
195+
if (written > 0)
189196
{
190-
Aws::String bufferStr(Aws::Utils::StringUtils::FromWString(buffer));
197+
Aws::String bufferStr(Aws::Utils::StringUtils::FromWString(buffer.data()));
191198
auto fileNameStart = bufferStr.find_last_of(PATH_DELIM);
192199
if (fileNameStart != std::string::npos)
193200
{

0 commit comments

Comments
 (0)