OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 // A fuzzer that checks correctness of json parser/writer. | |
6 // The fuzzer input is passed through parsing twice, | |
7 // so that presumably valid json is parsed/written again. | |
8 | |
9 #include <stddef.h> | |
10 #include <stdint.h> | |
11 | |
12 #include <string> | |
13 | |
14 #include "base/json/json_reader.h" | |
15 #include "base/json/json_writer.h" | |
16 #include "base/json/string_escape.h" | |
17 #include "base/logging.h" | |
18 #include "base/values.h" | |
19 | |
20 // Entry point for libFuzzer. | |
21 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { | |
22 // We will use the last byte of data as parsing options. | |
danakj
2016/10/31 20:02:09
nit: I suggest putting this on top of the function
| |
23 // The rest will be used as text input to the parser. | |
24 if (size < 2) | |
25 return 0; | |
26 | |
27 int error_code, error_line, error_column; | |
28 std::string error_message; | |
29 | |
30 const std::string input_string(reinterpret_cast<const char*>(data), size - 1); | |
31 const int options = data[size - 1]; | |
32 auto parsed_value = base::JSONReader::ReadAndReturnError( | |
33 input_string, options, &error_code, &error_message, &error_line, | |
34 &error_column); | |
35 if (!parsed_value) | |
36 return 0; | |
37 | |
38 std::string parsed_output; | |
39 bool b = base::JSONWriter::Write(*parsed_value, &parsed_output); | |
40 LOG_ASSERT(b); | |
41 | |
42 auto double_parsed_value = base::JSONReader::ReadAndReturnError( | |
43 parsed_output, options, &error_code, &error_message, &error_line, | |
44 &error_column); | |
45 LOG_ASSERT(double_parsed_value); | |
46 std::string double_parsed_output; | |
47 bool b2 = | |
48 base::JSONWriter::Write(*double_parsed_value, &double_parsed_output); | |
49 LOG_ASSERT(b2); | |
50 | |
51 LOG_ASSERT(parsed_output == double_parsed_output) | |
52 << "Parser/Writer mismatch." | |
53 << "\nInput=" << base::GetQuotedJSONString(parsed_output) | |
54 << "\nOutput=" << base::GetQuotedJSONString(double_parsed_output); | |
55 | |
56 return 0; | |
57 } | |
OLD | NEW |