Skip to content

Reject overlong UTF-8 forms in _Mbrtowc and add test case - #6060

Merged
Stephan T. Lavavej (StephanTLavavej) merged 5 commits into
microsoft:mainfrom
cpplearner:utf-8
Feb 11, 2026
Merged

Reject overlong UTF-8 forms in _Mbrtowc and add test case#6060
Stephan T. Lavavej (StephanTLavavej) merged 5 commits into
microsoft:mainfrom
cpplearner:utf-8

Conversation

@cpplearner

@cpplearner S. B. Tam (cpplearner) commented Jan 31, 2026

Copy link
Copy Markdown
Contributor

This PR adds test case on top of #5943, and removes the check for out-of-range code points.

While _Mbrtowc is used by both <locale> and <format> facilities, only the former are affected because the <format> functions call the constexpr function _Decode_utf if the encoding is UTF-8.

I removed the check for out-of-range values because I think wch can't exceed 0xFFFF: there's an early return if the result won't fit in the Basic Multilingual Plane,

STL/stl/src/xmbtowc.cpp

Lines 110 to 115 in 3f76681

if (trailingUtf8Units > 2) { // this would result in a UTF-16 surrogate pair, which we can't emit in our
// singular output wchar_t, so fail
// see N4950 [locale.codecvt.virtuals]/3
errno = EILSEQ;
return -1;
}

AZero13 (AZero13) and others added 3 commits December 6, 2025 10:39
Added UTF-8 validation in _Mbrtowc to reject overlong encodings and out-of-range code points, returning EILSEQ instead of accepting ill-formed sequences.
Comment thread tests/std/tests/VSO_0644691_utf_8_codecvt/test.cpp Outdated
@StephanTLavavej

Copy link
Copy Markdown
Member

Thank you so much for figuring out how to salvage this! 😻 Now I finally understand what the issue is. I pushed additional test coverage.

@StephanTLavavej Stephan T. Lavavej (StephanTLavavej) moved this from Initial Review to Ready To Merge in STL Code Reviews Jan 31, 2026
@StephanTLavavej Stephan T. Lavavej (StephanTLavavej) moved this from Ready To Merge to Merging in STL Code Reviews Feb 9, 2026
@StephanTLavavej

Copy link
Copy Markdown
Member

I'm mirroring this to the MSVC-internal repo. Please notify me if any further changes are pushed, otherwise no action is required.

@StephanTLavavej
Stephan T. Lavavej (StephanTLavavej) merged commit aaeb6b6 into microsoft:main Feb 11, 2026
45 checks passed
@github-project-automation github-project-automation Bot moved this from Merging to Done in STL Code Reviews Feb 11, 2026
@StephanTLavavej

Copy link
Copy Markdown
Member

😻 🎉 🚀

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

Labels

bug Something isn't working

4 participants