fix: ensure JSON output is valid UTF-8 - #3445
Closed
Wilfred wants to merge 1 commit into
Closed
Conversation
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
Lua strings are byte arrays, so a literal such as
"\x80"can contain bytes that are not valid UTF-8. The JSON encoder previously copied those bytes unchanged, producing JSON and LSP bodies that strict UTF-8 decoders reject.Validate strings at the JSON boundary and replace each invalid byte with U+FFFD before applying the existing JSON escapes. Valid UTF-8 is unchanged. Lua 5.1 and 5.2 keep the previous behavior because they do not provide
utf8.len.The repository itself contains a trigger in
script/parser/guide.lua:Requesting document symbols evaluates that literal and puts its byte-string value into
detail. Before this change, the response contained raw bytes0x80and0xff. Afterwards it contains replacement characters and the complete response is valid UTF-8.This intentionally fixes the serialization boundary only. Producer-specific escaping is a separate display policy and is not necessary to guarantee valid JSON output.
Related: #2983. The beautified JSON encoder used by documentation export shares the repaired string encoder and is covered by the new test.
Validation
3rd/luamake/luamake: all 110 bee tests pass (3 skipped), followed by the complete LuaLS test suite.initialize,didOpenforscript/parser/guide.lua, andtextDocument/documentSymbol; all 191,911 captured protocol bytes decode as UTF-8 and the trigger's detail is"[^%w_�-�]".Performance
A synthetic benchmark repeatedly encoded a document-symbol-shaped response with 250 symbols. Across five alternating runs, median time increased from 3.678s to 3.884s (+5.6%). Valid strings pay for the C
utf8.lenscan; allocation and repair occur only for invalid input.