Skip to content

Fix Windows GetEnv mangling non-ASCII environment values (UTF-8) - #3885

Open
kai-ion wants to merge 1 commit into
mainfrom
no-more-mojibake
Open

Fix Windows GetEnv mangling non-ASCII environment values (UTF-8)#3885
kai-ion wants to merge 1 commit into
mainfrom
no-more-mojibake

Conversation

@kai-ion

@kai-ion kai-ion commented Jul 30, 2026

Copy link
Copy Markdown
Collaborator

Issue #, if available:
#3520

Description of changes:
On Windows, Aws::Environment::GetEnv read variables through the narrow
_dupenv_s, which returns the value in the active ANSI code page (e.g.
CP-1252). Any non-ASCII value — an accented username in a home path, a
region/proxy set to a localized string — was then reinterpreted elsewhere in
the SDK as UTF-8 and came out as mojibake (e.g. JoséJos\xE9 instead of
Jos\xC3\xA9).

This changes the Windows implementation to read through the wide _wdupenv_s
and convert explicitly to UTF-8 via StringUtils::FromWString, since Windows
stores environment variables natively as UTF-16. This mirrors the same fix
made in the CRT (awslabs/aws-c-common#1260). _wdupenv_s is
MSVC-specific, so non-MSVC compilers keep the existing std::getenv path.

Added a regression test (FileSystemUtilsTest.GetEnvReturnsNonAsciiValueAsUtf8)
that injects a non-ASCII value via the wide CRT API (_wputenv_s, so the true
Unicode value is stored rather than a pre-mangled narrow one) and asserts
GetEnv returns the correct UTF-8 bytes. The test is Windows-only and skips
elsewhere.

Check all that applies:

  • Did a review by yourself.
  • Added proper tests to cover this PR. (If tests are not applicable, explain.)
  • Checked if this PR is a breaking (APIs have been changed) change.
  • Checked if this PR will not introduce cross-platform inconsistent behavior.
  • Checked if this PR would require a ReadMe/Wiki update.

Check which platforms you have built SDK on to verify the correctness of this PR.

  • Linux
  • Windows
  • Android
  • MacOS
  • IOS
  • Other Platforms

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@kai-ion
kai-ion force-pushed the no-more-mojibake branch 3 times, most recently from 7ac9b1e to 8718685 Compare July 30, 2026 21:21
On Windows, GetEnv read variables through the narrow _dupenv_s, which
returns the value in the active ANSI code page (e.g. CP-1252). Non-ASCII
values -- such as a home path with an accented username -- were then
reinterpreted as UTF-8 and came out mangled, so ~/.aws/credentials and
~/.aws/config failed to load and the profile provider fell back to an
anonymous request (GitHub issue #3865).

Read env vars through the wide _wdupenv_s and convert explicitly to
UTF-8, since Windows stores them natively as UTF-16. Mirrors the same
fix in the CRT (awslabs/aws-c-common#1260). Non-MSVC compilers keep the
existing std::getenv path.

Adds a Windows-only regression test that injects a non-ASCII value via
the wide CRT API and asserts GetEnv returns the correct UTF-8 bytes.
@kai-ion
kai-ion force-pushed the no-more-mojibake branch from 8718685 to 89408d2 Compare July 30, 2026 21:29

ASSERT_EQ(Aws::String("Jos\xC3\xA9"), value); // "José" in UTF-8
#else
GTEST_SKIP() << "Non-ASCII environment encoding fix is Windows-specific.";

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of skipping, just ifdef the whole test

#ifdef _MSC_VER
TEST_F(FileTest, GetEnvReturnsNonAsciiValueAsUtf8) {
  ....
}
#endif

we do that in several other places, i.e. http tests for libcurl only tests

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants