[fuzzer] Fix loading of schema in monster_fuzzer (#6308)

This is fix for (https://oss-fuzz.com/testcase-detail/6251772204810240)
This commit is contained in:
Vladimir Glavnyy
2020-12-08 02:21:23 +07:00
committed by GitHub
parent 7f33cf682a
commit f437f0f7ed
2 changed files with 23 additions and 35 deletions

View File

@@ -135,6 +135,12 @@ target_link_libraries(verifier_fuzzer PRIVATE flatbuffers_fuzzed)
add_executable(monster_fuzzer flatbuffers_monster_fuzzer.cc)
target_link_libraries(monster_fuzzer PRIVATE flatbuffers_fuzzed)
add_custom_command(
TARGET monster_fuzzer POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
${CMAKE_SOURCE_DIR}/../monster_test.bfbs
${CMAKE_CURRENT_BINARY_DIR}/monster_test.bfbs)
# Build debugger for weird cases found with fuzzer.
if(BUILD_DEBUGGER)

View File

@@ -25,49 +25,31 @@
#include "test_init.h"
namespace {
constexpr bool use_binary_schema = true;
// should point to flatbuffers/tests/
constexpr const char *test_data_path = "../../";
constexpr const char *schema_file_name = "monster_test";
static constexpr uint8_t flags_strict_json = 0x80;
static constexpr uint8_t flags_skip_unexpected_fields_in_json = 0x40;
static constexpr uint8_t flags_allow_non_utf8 = 0x20;
std::string LoadBinarySchema(const char *file_name) {
std::string schemafile;
TEST_EQ(true,
flatbuffers::LoadFile(file_name, true, &schemafile));
flatbuffers::Verifier verifier(
reinterpret_cast<const uint8_t *>(schemafile.c_str()),
schemafile.size());
TEST_EQ(true, reflection::VerifySchemaBuffer(verifier));
return schemafile;
}
flatbuffers::Parser make_parser(const flatbuffers::IDLOptions opts) {
// once loaded from disk
static const std::string schemafile = [&]() {
std::string schemafile;
TEST_EQ(
flatbuffers::LoadFile((std::string(test_data_path) + schema_file_name +
(use_binary_schema ? ".bfbs" : ".fbs"))
.c_str(),
use_binary_schema, &schemafile),
true);
if (use_binary_schema) {
flatbuffers::Verifier verifier(
reinterpret_cast<const uint8_t *>(schemafile.c_str()),
schemafile.size());
TEST_EQ(reflection::VerifySchemaBuffer(verifier), true);
}
return schemafile;
}();
static const std::string schemafile = LoadBinarySchema("./monster_test.bfbs");
// parse schema first, so we can use it to parse the data after
flatbuffers::Parser parser;
if (use_binary_schema) {
TEST_EQ(parser.Deserialize(
reinterpret_cast<const uint8_t *>(schemafile.c_str()),
schemafile.size()),
true);
} else {
auto include_test_path =
flatbuffers::ConCatPathFileName(test_data_path, "include_test");
const char *include_directories[] = { test_data_path,
include_test_path.c_str(), nullptr };
TEST_EQ(parser.Parse(schemafile.c_str(), include_directories), true);
}
TEST_EQ(true, parser.Deserialize(
reinterpret_cast<const uint8_t *>(schemafile.c_str()),
schemafile.size()));
// (re)define parser options
parser.opts = opts;
return parser;
@@ -80,7 +62,7 @@ std::string do_test(const flatbuffers::IDLOptions &opts,
if (parser.ParseJson(input_json.c_str())) {
flatbuffers::Verifier verifier(parser.builder_.GetBufferPointer(),
parser.builder_.GetSize());
TEST_EQ(MyGame::Example::VerifyMonsterBuffer(verifier), true);
TEST_EQ(true, MyGame::Example::VerifyMonsterBuffer(verifier));
TEST_ASSERT(
GenerateText(parser, parser.builder_.GetBufferPointer(), &jsongen));
}