google / google/fuzztest

Backslashes are wrongly escaped in grammar_codegen

Open
#915 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
1.1k
Forks
137
Avg merge
3d 6h
Merged PRs (30d)
22

Description

When I change [fuzztest/grammars/JSON.g4](https://github.com/google/fuzztest/blob/main/fuzztest/grammars/JSON.g4) to support some escape sequences
```antlr
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.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.