1 // Copyright 2015 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "base/json/json_reader.h" 6 #include "base/json/json_writer.h" 7 #include "base/values.h" 8 #include "third_party/abseil-cpp/absl/types/optional.h" 9 10 namespace base { 11 12 // Entry point for LibFuzzer. LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)13extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { 14 if (size < 2) 15 return 0; 16 17 // Create a copy of input buffer, as otherwise we don't catch 18 // overflow that touches the last byte (which is used in options). 19 std::unique_ptr<char[]> input(new char[size - 1]); 20 memcpy(input.get(), data, size - 1); 21 22 StringPiece input_string(input.get(), size - 1); 23 24 const int options = data[size - 1]; 25 26 auto json_val = 27 JSONReader::ReadAndReturnValueWithError(input_string, options); 28 if (json_val.has_value()) { 29 // Check that the value can be serialized and deserialized back to an 30 // equivalent |Value|. 31 const Value& value = *json_val; 32 std::string serialized; 33 CHECK(JSONWriter::Write(value, &serialized)); 34 35 absl::optional<Value> deserialized = 36 JSONReader::Read(StringPiece(serialized)); 37 CHECK(deserialized); 38 CHECK_EQ(value, deserialized.value()); 39 } 40 41 return 0; 42 } 43 44 } // namespace base 45