| 6 | #include "base/json/json_writer.h" |
| 7 | #include "base/values.h" |
| 8 |
| 9 | namespace base { |
| 10 |
| 11 | // Entry point for LibFuzzer. |
| 12 | extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { |
| 13 | if (size < 2) |
| 14 | return 0; |
| 15 |
| 16 | // Create a copy of input buffer, as otherwise we don't catch |
| 17 | // overflow that touches the last byte (which is used in options). |
| 18 | std::unique_ptr<char[]> input(new char[size - 1]); |
| 19 | memcpy(input.get(), data, size - 1); |
| 20 |
| 21 | StringPiece input_string(input.get(), size - 1); |
| 22 |
| 23 | const int options = data[size - 1]; |
| 24 |
| 25 | JSONReader::ValueWithError json_val = |
| 26 | JSONReader::ReadAndReturnValueWithError(input_string, options); |
| 27 | CHECK((json_val.error_code == JSONReader::JSON_NO_ERROR) == |
| 28 | json_val.value.has_value()); |
| 29 |
| 30 | if (json_val.value) { |
| 31 | // Check that the value can be serialized and deserialized back to an |
| 32 | // equivalent |Value|. |
| 33 | const Value& value = json_val.value.value(); |
| 34 | std::string serialized; |
| 35 | CHECK(JSONWriter::Write(value, &serialized)); |
| 36 |
| 37 | Optional<Value> deserialized = JSONReader::Read(StringPiece(serialized)); |
| 38 | CHECK(deserialized); |
| 39 | CHECK(value.Equals(&deserialized.value())); |
| 40 | } |
| 41 |
| 42 | return 0; |
| 43 | } |
| 44 |
| 45 | } // namespace base |
| 46 |