fix(render): encode non-BMP characters as UTF-16 surrogate pairs in AsciiJSON - #4728
Open
amitmishra11 wants to merge 1 commit into
Open
fix(render): encode non-BMP characters as UTF-16 surrogate pairs in AsciiJSON#4728amitmishra11 wants to merge 1 commit into
amitmishra11 wants to merge 1 commit into
Conversation
…sciiJSON AsciiJSON used fmt.Appendf with "\u%04x" to escape all non-ASCII runes. For code points above U+FFFF the format produces a 5+ digit sequence (e.g. ὠ0 for U+1F600), which is syntactically valid JSON but wrong: a JSON \u escape is exactly 4 hex digits, so decoders interpret the 5th digit as literal text and the recovered string is silently corrupted. Per RFC 8259 section 7, code points above U+FFFF must be encoded as a UTF-16 surrogate pair (\uD800-\uDBFF followed by \uDC00-\uDFFF). Use unicode/utf16.EncodeRune to compute the pair and emit two \uXXXX escapes. Add TestRenderAsciiJSONNonBMP to verify that emoji and other non-BMP characters survive an AsciiJSON -> json.Unmarshal round-trip unchanged. Fixes gin-gonic#4688 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #4728 +/- ##
==========================================
- Coverage 99.21% 98.33% -0.89%
==========================================
Files 42 48 +6
Lines 3182 3176 -6
==========================================
- Hits 3157 3123 -34
- Misses 17 43 +26
- Partials 8 10 +2
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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.
Bug
AsciiJSON silently corrupts any Unicode code point above U+FFFF (emoji, CJK Extension B, etc.). The escaping loop uses fmt.Appendf(buf, "\u%04x", r), but %04x is a minimum width, not a fixed width. A non-BMP rune produces a 5-digit escape such as ὠ0 instead of the required 4-digit form. JSON parsers read exactly 4 hex digits for \u, so the 5th digit becomes literal text and the decoded string is silently wrong.
Example: rendering the string consisting of U+1F600 (grinning face emoji):
Before fix: AsciiJSON output is {"msg":"ὠ0"}, json.Unmarshal gives "xE0" (wrong)
After fix: AsciiJSON output is {"msg":"😀"}, json.Unmarshal gives the original emoji (correct)
Reported in #4688.
Root cause
Per RFC 8259 section 7, code points outside the BMP must be encoded as a UTF-16 surrogate pair (\uHHHH\uLLLL). The existing code uses a single \uXXXX token regardless of the code point size.
Fix
Test
Added TestRenderAsciiJSONNonBMP in render/render_test.go. It checks several emoji and a CJK Extension B character for: