Fix UTF-8 surrogate pair encoding - #70
Closed
janrysavy wants to merge 1 commit into
Closed
Conversation
This patch is intended only as a reference for the author and for completeness. Pierre has been preparing his own upstream patches, so the important part here is the reproduced issue, the minimal code location, and the validation result. The UTF-16 to UTF-8 conversion path for surrogate pairs computed the correct Unicode code point, but used $e0 for the first byte of the four-byte UTF-8 sequence. For non-BMP diagnostic text such as U+1F600, this produced invalid bytes like E0 9F 98 80 instead of the valid F0 9F 98 80 sequence. Use $f0 for the leading byte of four-byte UTF-8 sequences. A standalone reproducer using FastMM_LogStateToFile with non-BMP diagnostic text was validated locally with RAD Studio 37.0 for Win32 and Win64 and is kept outside the upstream PR tree.
Owner
|
Thanks Jan, I actually just pushed a fix for this in response to the ChatGPT report you linked in your previous report. I am currently busy adding locking to the event log handling. |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
This is a small reference PR for completeness from the same review pass as #67 and #68.
I understand that you have been preparing your own upstream patches for the reported issues, so this PR is intended mainly as a concrete reference: it shows the reproduced behavior, the minimal affected code location, and the validation result.
This PR fixes the UTF-16 to UTF-8 conversion path for non-BMP diagnostic text.
Problem
ConvertUTF16toUTF8correctly recognizes UTF-16 surrogate pairs and computes the Unicode code point:However, the leading byte of the four-byte UTF-8 sequence used
$e0:Four-byte UTF-8 sequences must start with
11110xxx, so the leading mask should be$f0. With the current code, non-BMP text such as U+1F600 is written as invalid UTF-8 bytes:The valid UTF-8 sequence is:
This affects diagnostic text written through UTF-8 text-file output, for example
FastMM_LogStateToFileadditional details or other diagnostic messages containing non-BMP characters.Fix
Use
$f0for the leading byte of a four-byte UTF-8 sequence:The change is intentionally minimal and only affects UTF-8 encoding of UTF-16 surrogate pairs.
Reproducer
I kept the patch minimal and did not add a new test directory because the repository does not currently appear to have one. I can add this reproducer wherever you prefer.
Standalone reproducer:
Validation
Tested locally against upstream
1dad84bwith RAD Studio 37.0:dcc64: reproduced the invalidE0 9F 98 80sequence before the fix; build passed and reproducer passed after the fixDCC32: build passed and reproducer passed after the fixExpected successful output:
Risk
Low. The change only affects UTF-8 output for UTF-16 surrogate pairs. BMP characters, ASCII text, UTF-16LE output, and allocator behavior are unchanged.