-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Fix decoder on broken utf8 sequences. #3044
Merged
Merged
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 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 |
---|---|---|
|
@@ -602,6 +602,7 @@ FMT_CONSTEXPR FMT_NOINLINE auto copy_str_noinline(InputIt begin, InputIt end, | |
*/ | ||
FMT_CONSTEXPR inline auto utf8_decode(const char* s, uint32_t* c, int* e) | ||
-> const char* { | ||
constexpr const int prefix_masks[] = {0x00, 0x80, 0xe0, 0xf0, 0xf8}; | ||
constexpr const int masks[] = {0x00, 0x7f, 0x1f, 0x0f, 0x07}; | ||
constexpr const uint32_t mins[] = {4194304, 0, 128, 2048, 65536}; | ||
constexpr const int shiftc[] = {0, 18, 12, 6, 0}; | ||
|
@@ -628,6 +629,8 @@ FMT_CONSTEXPR inline auto utf8_decode(const char* s, uint32_t* c, int* e) | |
*e |= uchar(s[3]) >> 6; | ||
*e ^= 0x2a; // top two bits of each tail byte correct? | ||
*e >>= shifte[len]; | ||
*e |= ((uchar(s[0]) & prefix_masks[len]) != | ||
uchar((prefix_masks[len] << 1) & 0xFF)); // first byte correct? | ||
|
||
return next; | ||
} | ||
|
@@ -643,8 +646,8 @@ FMT_CONSTEXPR void for_each_codepoint(string_view s, F f) { | |
auto error = 0; | ||
auto end = utf8_decode(buf_ptr, &cp, &error); | ||
bool result = f(error ? invalid_code_point : cp, | ||
string_view(ptr, to_unsigned(end - buf_ptr))); | ||
return result ? end : nullptr; | ||
string_view(ptr, error ? 1 : to_unsigned(end - buf_ptr))); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we saw error here "end - buf_ptr" is < 0, but I dont follow how that could happen with invalid code point. |
||
return result ? (error ? buf_ptr + 1 : end) : nullptr; | ||
}; | ||
auto p = s.data(); | ||
const size_t block_size = 4; // utf8_decode always reads blocks of 4 chars. | ||
|
This file contains 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
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.
I don't think this is needed because
utf8_decode
already gives an error for "\xf0\x28...".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.
Without this check failed at sequence
\xf4\x8f\xbf\xc0
.utf8_decode
return zero ine
.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.
Could you add a test case then?
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.
Nevermind, I missed the code snippet which shows it's already there.
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 already present:
fmt/test/ranges-test.cc
Lines 383 to 384 in 541cd21
This patch fix two error.
First error fixed by add condition
error ? 1 : to_unsigned(end - buf_ptr)
Second - by add first byte check.
The first error masked the second on the sequence
\xf4\x8f\xbf\xc0
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.
AFAICS the current version of
utf8_decode
already returns an error for "\xf4\x8f\xbf\xc0" too: https://godbolt.org/z/E194qvshKThere 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.
Step 1:
"\xf4\x8f\xbf\xc0"
- is not valid utf-8, print\xf4
, remove\xf4
from sequence.Step 2:
"\x8f\xbf\xc0\0"
- is not valid utf-8, but utf8_decode return no error and invalid codepoint (15, not 143).See: https://godbolt.org/z/EbeEex4Gf
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.
Makes sense, thanks.