Skip to content

Backslashes are wrongly escaped in grammar_codegen #915

Open

Description

When I change fuzztest/grammars/JSON.g4 to support some escape sequences

STRING : '"' (CHARACTER | '\\' ESCAPED )* '"' ;

ESCAPED : '"' | '\\' | '/' | 'b' | 'f' | 'n' | 'r' | 't' ;

then the grammar tests fail because the nlohmann/json library fails to parse the generated json:

$ bazel test //domain_tests:in_grammar_domain_test
...
Executing tests from //domain_tests:in_grammar_domain_test
-----------------------------------------------------------------------------
Running main() from gmock_main.cc
[==========] Running 6 tests from 2 test suites.
[----------] Global test environment set-up.
[----------] 5 tests from InJsonGrammar
[ RUN      ] InJsonGrammar.InitGeneratesDifferentValidJson
domain_tests/in_grammar_domain_test.cc:41: Failure
Value of: parsed_json.is_discarded()
  Actual: true
Expected: false
"{\"\\\\\"\":[80.0E3,false]}"
...

That's because backslashes are escaped when they shouldn't be. E.g. "\"" (quote, escaped quote, quote) becomes "\\"" (quote, escaped backslash, quote, quote), which is not correct json.

The linked PR fixes the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions