-
Notifications
You must be signed in to change notification settings - Fork 3.2k
avoid partial UTF-8 sequences in wide-string AppendTruncated #2119
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
Open
nabhan06
wants to merge
2
commits into
abseil:master
Choose a base branch
from
nabhan06:log-wide-surrogate-truncation
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+135
−4
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| // Copyright 2025 The Abseil Authors | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // https://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #include "absl/log/internal/append_truncated.h" | ||
|
|
||
| #include <array> | ||
| #include <cstddef> | ||
| #include <string> | ||
| #include <string_view> | ||
|
|
||
| #include "gtest/gtest.h" | ||
| #include "absl/types/span.h" | ||
|
|
||
| namespace { | ||
|
|
||
| using ::absl::log_internal::AppendTruncated; | ||
|
|
||
| // Runs `AppendTruncated(src, ...)` against a `capacity`-byte buffer and returns | ||
| // the bytes it wrote. | ||
| std::string Append(std::wstring_view src, size_t capacity) { | ||
| std::array<char, 64> buffer{}; | ||
| absl::Span<char> dst(buffer.data(), capacity); | ||
| const size_t bytes_written = AppendTruncated(src, dst); | ||
| EXPECT_LE(bytes_written, capacity); | ||
| return std::string(buffer.data(), bytes_written); | ||
| } | ||
|
|
||
| const std::string kReplacement = "\xEF\xBF\xBD"; // U+FFFD | ||
|
|
||
| TEST(AppendTruncatedTest, EncodesSurrogatePair) { | ||
| // U+1F600, as a UTF-16 pair on platforms with a 2-byte wchar_t. | ||
| EXPECT_EQ(Append(L"\xD83D\xDE00", 32), "\xF0\x9F\x98\x80"); | ||
| } | ||
|
|
||
| TEST(AppendTruncatedTest, TrailingUnpairedHighSurrogate) { | ||
| // The high surrogate encodes only the first two bytes of a four-byte | ||
| // sequence, so emitting it alone would leave a partial sequence behind. | ||
| EXPECT_EQ(Append(L"\xD800", 32), kReplacement); | ||
| } | ||
|
|
||
| TEST(AppendTruncatedTest, HighSurrogateNotFollowedByLowSurrogate) { | ||
| const std::wstring high_then_ascii = std::wstring(1, wchar_t{0xD800}) + L"A"; | ||
| EXPECT_EQ(Append(high_then_ascii, 32), kReplacement + "A"); | ||
| EXPECT_EQ(Append(L"\xD800\xD801", 32), kReplacement + kReplacement); | ||
| } | ||
|
|
||
| TEST(AppendTruncatedTest, IsolatedLowSurrogate) { | ||
| EXPECT_EQ(Append(L"\xDC00", 32), kReplacement); | ||
| } | ||
|
|
||
| TEST(AppendTruncatedTest, SurrogatePairAtTruncationBoundary) { | ||
| const std::wstring src = | ||
| std::wstring(L"a") + wchar_t{0xD83D} + wchar_t{0xDE00}; | ||
| // Five bytes is exactly enough for "a" plus the four-byte sequence. | ||
| EXPECT_EQ(Append(src, 5), "a\xF0\x9F\x98\x80"); | ||
| EXPECT_EQ(Append(src, 6), "a\xF0\x9F\x98\x80"); | ||
| // Four bytes is not, so the pair is dropped rather than half-written. | ||
| EXPECT_EQ(Append(src, 4), "a"); | ||
| } | ||
|
|
||
| TEST(AppendTruncatedTest, PlainCharactersAreUnaffected) { | ||
| EXPECT_EQ(Append(L"hello", 32), "hello"); | ||
| // Spell the wide literals with universal character names so the encoding | ||
| // does not depend on the source file code page. MSVC decodes narrow source | ||
| // bytes with the system code page unless /utf-8 is passed, which would make | ||
| // a literal "\u00e9\u4e2d" here map to the wrong wchar_t values. | ||
| EXPECT_EQ(Append(L"\u00E9\u4E2D", 32), "\xC3\xA9\xE4\xB8\xAD"); | ||
| } | ||
|
|
||
| } // namespace | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test appears to be broken on windows
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch. The raw é中 literal was being decoded with the system code page on MSVC (no /utf-8), so the wchar_t values came out wrong. Switched it to universal character names (L"\u00E9\u4E2D") so the encoding no longer depends on the source file code page. The rest of the tests only use ASCII \x escapes, so they weren't affected.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch. The é中 literal was the problem: MSVC decodes the narrow source bytes with the system code page unless /utf-8 is passed, so those wchar_t values came out wrong on Windows. I switched it to universal character names (L"\u00E9\u4E2D"), which pin the code points regardless of source or execution charset. Pushed.