Skip to content

Fix incorrect usage of GetModuleFileNameW win api #2670

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 15, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions src/aws-cpp-sdk-core/source/platform/windows/FileSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <aws/core/platform/Environment.h>
#include <aws/core/utils/logging/LogMacros.h>
#include <aws/core/utils/StringUtils.h>
#include <aws/core/utils/memory/stl/AWSVector.h>
#include <cassert>
#include <iostream>
#include <Userenv.h>
Expand Down Expand Up @@ -180,14 +181,20 @@ Aws::String GetHomeDirectory()

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

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

if (GetModuleFileNameW(nullptr, buffer, static_cast<DWORD>(sizeof(buffer))))
if (written > 0)
{
Aws::String bufferStr(Aws::Utils::StringUtils::FromWString(buffer));
Aws::String bufferStr(Aws::Utils::StringUtils::FromWString(buffer.data()));
auto fileNameStart = bufferStr.find_last_of(PATH_DELIM);
if (fileNameStart != std::string::npos)
{
Expand Down