Chromium Code Reviews| 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 if (size < 2) | |
| 23 return 0; | |
| 24 | |
| 25 int error_code, error_line, error_column; | |
| 26 std::string error_message; | |
| 27 | |
| 28 const std::string input_string(reinterpret_cast<const char*>(data), size - 1); | |
| 29 const int options = data[size - 1]; | |
|
danakj
2016/10/29 01:12:42
Still wondering tho, where is this input format fo
aizatsky
2016/10/31 19:56:18
done
| |
| 30 auto parsed_value = base::JSONReader::ReadAndReturnError( | |
| 31 input_string, options, &error_code, &error_message, &error_line, | |
| 32 &error_column); | |
| 33 if (!parsed_value) | |
| 34 return 0; | |
| 35 | |
| 36 std::string parsed_output; | |
| 37 bool b = base::JSONWriter::Write(*parsed_value, &parsed_output); | |
| 38 LOG_ASSERT(b); | |
| 39 | |
| 40 auto double_parsed_value = base::JSONReader::ReadAndReturnError( | |
| 41 parsed_output, options, &error_code, &error_message, &error_line, | |
| 42 &error_column); | |
| 43 LOG_ASSERT(double_parsed_value); | |
| 44 std::string double_parsed_output; | |
| 45 bool b2 = | |
| 46 base::JSONWriter::Write(*double_parsed_value, &double_parsed_output); | |
| 47 LOG_ASSERT(b2); | |
| 48 | |
| 49 LOG_ASSERT(parsed_output == double_parsed_output) | |
| 50 << "Parser/Writer mismatch." | |
| 51 << "\nInput=" << base::GetQuotedJSONString(parsed_output) | |
| 52 << "\nOutput=" << base::GetQuotedJSONString(double_parsed_output); | |
| 53 | |
| 54 return 0; | |
| 55 } | |
| OLD | NEW |