Fix escaped-quote detection in JSON cleanup for strings ending with a backslash - #755
Draft
rootkiller6788 wants to merge 1 commit into
Draft
Conversation
… backslash cleanup_json detected an escaped quote by looking at only the single preceding character, so a closing quote that follows an escaped backslash (e.g. a string value ending in a literal backslash) was misread as an escaped quote. This caused otherwise-valid JSON to fail with 'Malformated JSON: missing 1 closing curly braces'. Count consecutive backslashes and treat the quote as escaped only when the count is odd.
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.
Problem
cleanup_jsoninlangfun/core/structured/schema/json.pydetects an escaped double quote by checking only the immediately preceding character:This is incorrect when a closing quote is preceded by an escaped backslash. In JSON,
\\"(two backslashes followed by a quote) means the string value ends with a literal backslash and the quote is a real closing quote. The old check treated that quote as escaped, so the scanner never left the string state and the trailing}was skipped, producing:This affects any JSON-protocol
lf.parse/lf.queryresponse whose string value ends with a backslash (for example a Windows path like"C:\\", a regex fragment, or a LaTeX string).Fix
Replace the single-character lookback with a helper that counts consecutive backslashes. A quote is escaped only when it is preceded by an odd number of backslashes, which is the correct JSON rule:
Test
Added
test_parse_with_escaped_backslashverifying that{"result": "C:\\"}parses toC:\(previously raisedJsonError). Existing tests, including the escaped-quote case intest_parse_basics, continue to pass.