mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-23 03:10:01 +00:00
Add fuzzer test for scalar numbers in json. (#4996)
* Add fuzzer test for scalar numbers in json. Grammar-based regex used to check correctness. * Fix conversation
This commit is contained in:
committed by
Wouter van Oortmerssen
parent
efbb11e093
commit
55b30827f2
@@ -3,14 +3,67 @@
|
||||
// found in the LICENSE file.
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <clocale>
|
||||
#include <string>
|
||||
|
||||
#include "flatbuffers/idl.h"
|
||||
|
||||
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
|
||||
flatbuffers::Parser parser;
|
||||
// Guarantee 0-termination.
|
||||
std::string s(reinterpret_cast<const char *>(data), size);
|
||||
parser.Parse(s.c_str());
|
||||
static constexpr uint8_t flags_strict_json = 0x01;
|
||||
static constexpr uint8_t flags_skip_unexpected_fields_in_json = 0x02;
|
||||
static constexpr uint8_t flags_allow_non_utf8 = 0x04;
|
||||
// static constexpr uint8_t flags_flag_3 = 0x08;
|
||||
// static constexpr uint8_t flags_flag_4 = 0x10;
|
||||
// static constexpr uint8_t flags_flag_5 = 0x20;
|
||||
// static constexpr uint8_t flags_flag_6 = 0x40;
|
||||
// static constexpr uint8_t flags_flag_7 = 0x80;
|
||||
|
||||
// See readme.md and CMakeLists.txt for details.
|
||||
#ifdef FUZZ_TEST_LOCALE
|
||||
static constexpr const char *test_locale = (FUZZ_TEST_LOCALE);
|
||||
#else
|
||||
static constexpr const char *test_locale = nullptr;
|
||||
#endif
|
||||
|
||||
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
|
||||
// Reserve one byte for Parser flags and one byte for repetition counter.
|
||||
if (size < 3) return 0;
|
||||
const uint8_t flags = data[0];
|
||||
// normalize to ascii alphabet
|
||||
const int extra_rep_number = data[1] >= '0' ? (data[1] - '0') : 0;
|
||||
data += 2;
|
||||
size -= 2; // bypass
|
||||
|
||||
const std::string original(reinterpret_cast<const char *>(data), size);
|
||||
auto input = std::string(original.c_str()); // until '\0'
|
||||
if (input.empty()) return 0;
|
||||
|
||||
flatbuffers::IDLOptions opts;
|
||||
opts.strict_json = (flags & flags_strict_json);
|
||||
opts.skip_unexpected_fields_in_json =
|
||||
(flags & flags_skip_unexpected_fields_in_json);
|
||||
opts.allow_non_utf8 = (flags & flags_allow_non_utf8);
|
||||
|
||||
flatbuffers::Parser parser(opts);
|
||||
|
||||
// Guarantee 0-termination in the input.
|
||||
auto parse_input = input.c_str();
|
||||
|
||||
// The fuzzer can adjust the number repetition if a side-effects have found.
|
||||
// Each test should pass at least two times to ensure that the parser doesn't
|
||||
// have any hidden-states or locale-depended effects.
|
||||
for (auto cnt = 0; cnt < (extra_rep_number + 2); cnt++) {
|
||||
auto use_locale = !!test_locale && (0 == (cnt % 2));
|
||||
// Set new locale.
|
||||
if (use_locale) {
|
||||
FLATBUFFERS_ASSERT(!!std::setlocale(LC_ALL, test_locale));
|
||||
}
|
||||
|
||||
// Check Parser.
|
||||
parser.Parse(parse_input);
|
||||
|
||||
// Restore locale.
|
||||
if (use_locale) { FLATBUFFERS_ASSERT(!!std::setlocale(LC_ALL, "C")); }
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user