-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfuzz_schema.cpp
More file actions
41 lines (33 loc) · 1.63 KB
/
Copy pathfuzz_schema.cpp
File metadata and controls
41 lines (33 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Copyright 2025 ByteDance Ltd. and/or its affiliates. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
#include "fuzz_util.h"
#include <cstddef>
#include <cstdint>
#include <string>
#include <vector>
using ByteDance::pjson;
// Schema-validation consistency.
// Splits a fuzz case into a schema and document, then compares validation overloads.
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
if (size > pjson_fuzz::kMaxInputBytes)
return 0;
// Prefer `schema\ndocument` framing; unframed inputs are divided at their midpoint.
const std::string input(pjson_fuzz::bytes(data, size), size);
std::string schemaInput;
std::string documentInput;
pjson_fuzz::splitOnNewlineOrMidpoint(input, schemaInput, documentInput);
// Only pairs that are both valid strict JSON values can exercise schema validation.
const pjson::ParseOptions options = pjson_fuzz::parseOptionsVariant(data, size, 0U);
pjson::unique_ptr schema = pjson::parse(schemaInput, options);
pjson::unique_ptr document = pjson::parse(documentInput, options);
if (!schema || !document)
return 0;
// Detailed and simple validation must agree, and errors exist exactly on failure.
const pjson::SchemaOptions schemaOptions = pjson_fuzz::boundedSchemaOptions(data, size, 4U);
std::vector<pjson::SchemaError> errors;
const bool detailed = document->validate(*schema, errors, schemaOptions);
const bool simple = document->validate(*schema, schemaOptions);
pjson_fuzz::require(simple == detailed);
pjson_fuzz::require(detailed == errors.empty());
return 0;
}