Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C++: Add option to skip verifying nested flatbuffers #7489

Merged
merged 6 commits into from Aug 29, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 18 additions & 2 deletions include/flatbuffers/verifier.h
Expand Up @@ -25,15 +25,27 @@ namespace flatbuffers {
// Helper class to verify the integrity of a FlatBuffer
class Verifier FLATBUFFERS_FINAL_CLASS {
public:
struct Options {
uoffset_t max_depth = 64;
dbaileychess marked this conversation as resolved.
Show resolved Hide resolved
uoffset_t max_tables = 1000000;
bool check_alignment = true;
bool check_nested_flatbuffers = true;
};
Verifier(const uint8_t *const buf, const size_t buf_len, Options opts)
dbaileychess marked this conversation as resolved.
Show resolved Hide resolved
: Verifier(buf, buf_len, opts.max_depth, opts.max_tables,
opts.check_alignment, opts.check_nested_flatbuffers) {}

Verifier(const uint8_t *const buf, const size_t buf_len,
dbaileychess marked this conversation as resolved.
Show resolved Hide resolved
const uoffset_t _max_depth = 64,
const uoffset_t _max_tables = 1000000,
const bool _check_alignment = true)
const bool _check_alignment = true,
const bool _check_nested_flatbuffers = true)
dbaileychess marked this conversation as resolved.
Show resolved Hide resolved
: buf_(buf),
size_(buf_len),
max_depth_(_max_depth),
max_tables_(_max_tables),
check_alignment_(_check_alignment),
check_nested_flatbuffers_(_check_nested_flatbuffers),
upper_bound_(0),
depth_(0),
num_tables_(0),
Expand Down Expand Up @@ -204,11 +216,14 @@ class Verifier FLATBUFFERS_FINAL_CLASS {
template<typename T>
bool VerifyNestedFlatBuffer(const Vector<uint8_t> *const buf,
const char *const identifier) {
// Caller opted out of this.
if (!check_nested_flatbuffers_) return true;

// An empty buffer is OK as it indicates not present.
if (!buf) return true;

// If there is a nested buffer, it must be greater than the min size.
if(!Check(buf->size() >= FLATBUFFERS_MIN_BUFFER_SIZE)) return false;
if (!Check(buf->size() >= FLATBUFFERS_MIN_BUFFER_SIZE)) return false;

Verifier nested_verifier(buf->data(), buf->size());
return nested_verifier.VerifyBuffer<T>(identifier);
Expand Down Expand Up @@ -291,6 +306,7 @@ class Verifier FLATBUFFERS_FINAL_CLASS {
const uoffset_t max_depth_;
dbaileychess marked this conversation as resolved.
Show resolved Hide resolved
const uoffset_t max_tables_;
const bool check_alignment_;
const bool check_nested_flatbuffers_;

mutable size_t upper_bound_;

Expand Down
8 changes: 8 additions & 0 deletions tests/test.cpp
Expand Up @@ -1084,6 +1084,14 @@ void NestedVerifierTest() {
flatbuffers::Verifier verifier(builder.GetBufferPointer(),
builder.GetSize());
TEST_EQ(false, VerifyMonsterBuffer(verifier));

// Verify the root monster succeeds, since we've disabled checking nested
// flatbuffers
flatbuffers::Verifier::Options options;
options.check_nested_flatbuffers = false;
flatbuffers::Verifier no_check_nested(builder.GetBufferPointer(),
builder.GetSize(), options);
TEST_EQ(true, VerifyMonsterBuffer(no_check_nested));
}

{
Expand Down