Made FLATBUFFERS_TRACK_VERIFIER_BUFFER_SIZE #ifdef out less

This is to protect against cases where part of a project is
compiled with or without this flag, making for very fragile
and hard to find bugs, such as sizeof(Verifier) changing.

Change-Id: I01c895cdc5b44f860e4b0b9c9613bff1983e2b9d
See: https://bugs.chromium.org/p/chromium/issues/detail?id=929847
parent 600f3fbc
...@@ -1977,13 +1977,9 @@ class Verifier FLATBUFFERS_FINAL_CLASS { ...@@ -1977,13 +1977,9 @@ class Verifier FLATBUFFERS_FINAL_CLASS {
depth_(0), depth_(0),
max_depth_(_max_depth), max_depth_(_max_depth),
num_tables_(0), num_tables_(0),
max_tables_(_max_tables) max_tables_(_max_tables),
// clang-format off upper_bound_(0),
#ifdef FLATBUFFERS_TRACK_VERIFIER_BUFFER_SIZE check_alignment_(_check_alignment)
, upper_bound_(0)
#endif
, check_alignment_(_check_alignment)
// clang-format on
{ {
FLATBUFFERS_ASSERT(size_ < FLATBUFFERS_MAX_BUFFER_SIZE); FLATBUFFERS_ASSERT(size_ < FLATBUFFERS_MAX_BUFFER_SIZE);
} }
...@@ -2174,17 +2170,21 @@ class Verifier FLATBUFFERS_FINAL_CLASS { ...@@ -2174,17 +2170,21 @@ class Verifier FLATBUFFERS_FINAL_CLASS {
return true; return true;
} }
// clang-format off
#ifdef FLATBUFFERS_TRACK_VERIFIER_BUFFER_SIZE
// Returns the message size in bytes // Returns the message size in bytes
size_t GetComputedSize() const { size_t GetComputedSize() const {
uintptr_t size = upper_bound_; // clang-format off
// Align the size to uoffset_t #ifdef FLATBUFFERS_TRACK_VERIFIER_BUFFER_SIZE
size = (size - 1 + sizeof(uoffset_t)) & ~(sizeof(uoffset_t) - 1); uintptr_t size = upper_bound_;
return (size > size_) ? 0 : size; // Align the size to uoffset_t
size = (size - 1 + sizeof(uoffset_t)) & ~(sizeof(uoffset_t) - 1);
return (size > size_) ? 0 : size;
#else
// Must turn on FLATBUFFERS_TRACK_VERIFIER_BUFFER_SIZE for this to work.
FLATBUFFERS_ASSERT(false);
return 0;
#endif
// clang-format on
} }
#endif
// clang-format on
private: private:
const uint8_t *buf_; const uint8_t *buf_;
...@@ -2193,11 +2193,7 @@ class Verifier FLATBUFFERS_FINAL_CLASS { ...@@ -2193,11 +2193,7 @@ class Verifier FLATBUFFERS_FINAL_CLASS {
uoffset_t max_depth_; uoffset_t max_depth_;
uoffset_t num_tables_; uoffset_t num_tables_;
uoffset_t max_tables_; uoffset_t max_tables_;
// clang-format off mutable size_t upper_bound_;
#ifdef FLATBUFFERS_TRACK_VERIFIER_BUFFER_SIZE
mutable size_t upper_bound_;
#endif
// clang-format on
bool check_alignment_; bool check_alignment_;
}; };
......
...@@ -210,18 +210,22 @@ void AccessFlatBufferTest(const uint8_t *flatbuf, size_t length, ...@@ -210,18 +210,22 @@ void AccessFlatBufferTest(const uint8_t *flatbuf, size_t length,
flatbuffers::Verifier verifier(flatbuf, length); flatbuffers::Verifier verifier(flatbuf, length);
TEST_EQ(VerifyMonsterBuffer(verifier), true); TEST_EQ(VerifyMonsterBuffer(verifier), true);
std::vector<uint8_t> test_buff; // clang-format off
test_buff.resize(length * 2); #ifdef FLATBUFFERS_TRACK_VERIFIER_BUFFER_SIZE
std::memcpy(&test_buff[0], flatbuf, length); std::vector<uint8_t> test_buff;
std::memcpy(&test_buff[length], flatbuf, length); test_buff.resize(length * 2);
std::memcpy(&test_buff[0], flatbuf, length);
flatbuffers::Verifier verifier1(&test_buff[0], length); std::memcpy(&test_buff[length], flatbuf, length);
TEST_EQ(VerifyMonsterBuffer(verifier1), true);
TEST_EQ(verifier1.GetComputedSize(), length); flatbuffers::Verifier verifier1(&test_buff[0], length);
TEST_EQ(VerifyMonsterBuffer(verifier1), true);
flatbuffers::Verifier verifier2(&test_buff[length], length); TEST_EQ(verifier1.GetComputedSize(), length);
TEST_EQ(VerifyMonsterBuffer(verifier2), true);
TEST_EQ(verifier2.GetComputedSize(), length); flatbuffers::Verifier verifier2(&test_buff[length], length);
TEST_EQ(VerifyMonsterBuffer(verifier2), true);
TEST_EQ(verifier2.GetComputedSize(), length);
#endif
// clang-format on
TEST_EQ(strcmp(MonsterIdentifier(), "MONS"), 0); TEST_EQ(strcmp(MonsterIdentifier(), "MONS"), 0);
TEST_EQ(MonsterBufferHasIdentifier(flatbuf), true); TEST_EQ(MonsterBufferHasIdentifier(flatbuf), true);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment