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.
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"
12 // Entry point for LibFuzzer.
13 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
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);
22 StringPiece input_string(input.get(), size - 1);
24 const int options = data[size - 1];
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));
35 absl::optional<Value> deserialized =
36 JSONReader::Read(StringPiece(serialized));
38 CHECK_EQ(value, deserialized.value());