mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-03 20:31:23 +00:00
Size verifier fix 2 (#8740)
* Fixes to make SizeVerifier work. In particular change all the places in the Flatbuffers library and generated code that were using `Verifier` to instead use `VerifierTemplate<TrackBufferSize>` and wrap them all inside `template <bool TrackBufferSize = false>`. Also add unit tests for SizeVerifier. * Format using `sh scripts/clang-format-git.sh` * Use `B` rather than `TrackBufferSize` for the name of the template parameter. * Update generated files.
This commit is contained in:
@@ -158,8 +158,10 @@ struct CharacterUnion {
|
||||
}
|
||||
};
|
||||
|
||||
bool VerifyCharacter(::flatbuffers::Verifier &verifier, const void *obj, Character type);
|
||||
bool VerifyCharacterVector(::flatbuffers::Verifier &verifier, const ::flatbuffers::Vector<::flatbuffers::Offset<void>> *values, const ::flatbuffers::Vector<Character> *types);
|
||||
template <bool B = false>
|
||||
bool VerifyCharacter(::flatbuffers::VerifierTemplate<B> &verifier, const void *obj, Character type);
|
||||
template <bool B = false>
|
||||
bool VerifyCharacterVector(::flatbuffers::VerifierTemplate<B> &verifier, const ::flatbuffers::Vector<::flatbuffers::Offset<void>> *values, const ::flatbuffers::Vector<Character> *types);
|
||||
|
||||
enum class Gadget : uint8_t {
|
||||
NONE = 0,
|
||||
@@ -266,8 +268,10 @@ struct GadgetUnion {
|
||||
}
|
||||
};
|
||||
|
||||
bool VerifyGadget(::flatbuffers::Verifier &verifier, const void *obj, Gadget type);
|
||||
bool VerifyGadgetVector(::flatbuffers::Verifier &verifier, const ::flatbuffers::Vector<::flatbuffers::Offset<void>> *values, const ::flatbuffers::Vector<Gadget> *types);
|
||||
template <bool B = false>
|
||||
bool VerifyGadget(::flatbuffers::VerifierTemplate<B> &verifier, const void *obj, Gadget type);
|
||||
template <bool B = false>
|
||||
bool VerifyGadgetVector(::flatbuffers::VerifierTemplate<B> &verifier, const ::flatbuffers::Vector<::flatbuffers::Offset<void>> *values, const ::flatbuffers::Vector<Gadget> *types);
|
||||
|
||||
FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(4) Rapunzel FLATBUFFERS_FINAL_CLASS {
|
||||
private:
|
||||
@@ -418,7 +422,8 @@ struct Attacker FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table {
|
||||
if constexpr (Index == 0) return sword_attack_damage();
|
||||
else static_assert(Index != -1, "Invalid Field Index");
|
||||
}
|
||||
bool Verify(::flatbuffers::Verifier &verifier) const {
|
||||
template <bool B = false>
|
||||
bool Verify(::flatbuffers::VerifierTemplate<B> &verifier) const {
|
||||
return VerifyTableStart(verifier) &&
|
||||
VerifyField<int32_t>(verifier, VT_SWORD_ATTACK_DAMAGE, 4) &&
|
||||
verifier.EndTable();
|
||||
@@ -495,7 +500,8 @@ struct HandFan FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table {
|
||||
if constexpr (Index == 0) return length();
|
||||
else static_assert(Index != -1, "Invalid Field Index");
|
||||
}
|
||||
bool Verify(::flatbuffers::Verifier &verifier) const {
|
||||
template <bool B = false>
|
||||
bool Verify(::flatbuffers::VerifierTemplate<B> &verifier) const {
|
||||
return VerifyTableStart(verifier) &&
|
||||
VerifyField<int32_t>(verifier, VT_LENGTH, 4) &&
|
||||
verifier.EndTable();
|
||||
@@ -612,7 +618,8 @@ struct Movie FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table {
|
||||
else if constexpr (Index == 3) return characters();
|
||||
else static_assert(Index != -1, "Invalid Field Index");
|
||||
}
|
||||
bool Verify(::flatbuffers::Verifier &verifier) const {
|
||||
template <bool B = false>
|
||||
bool Verify(::flatbuffers::VerifierTemplate<B> &verifier) const {
|
||||
return VerifyTableStart(verifier) &&
|
||||
VerifyField<uint8_t>(verifier, VT_MAIN_CHARACTER_TYPE, 1) &&
|
||||
VerifyOffset(verifier, VT_MAIN_CHARACTER) &&
|
||||
@@ -791,7 +798,8 @@ inline ::flatbuffers::Offset<Movie> Movie::Pack(::flatbuffers::FlatBufferBuilder
|
||||
_characters);
|
||||
}
|
||||
|
||||
inline bool VerifyCharacter(::flatbuffers::Verifier &verifier, const void *obj, Character type) {
|
||||
template <bool B>
|
||||
inline bool VerifyCharacter(::flatbuffers::VerifierTemplate<B> &verifier, const void *obj, Character type) {
|
||||
switch (type) {
|
||||
case Character::NONE: {
|
||||
return true;
|
||||
@@ -801,13 +809,13 @@ inline bool VerifyCharacter(::flatbuffers::Verifier &verifier, const void *obj,
|
||||
return verifier.VerifyTable(ptr);
|
||||
}
|
||||
case Character::Rapunzel: {
|
||||
return verifier.VerifyField<Rapunzel>(static_cast<const uint8_t *>(obj), 0, 4);
|
||||
return verifier.template VerifyField<Rapunzel>(static_cast<const uint8_t *>(obj), 0, 4);
|
||||
}
|
||||
case Character::Belle: {
|
||||
return verifier.VerifyField<BookReader>(static_cast<const uint8_t *>(obj), 0, 4);
|
||||
return verifier.template VerifyField<BookReader>(static_cast<const uint8_t *>(obj), 0, 4);
|
||||
}
|
||||
case Character::BookFan: {
|
||||
return verifier.VerifyField<BookReader>(static_cast<const uint8_t *>(obj), 0, 4);
|
||||
return verifier.template VerifyField<BookReader>(static_cast<const uint8_t *>(obj), 0, 4);
|
||||
}
|
||||
case Character::Other: {
|
||||
auto ptr = reinterpret_cast<const ::flatbuffers::String *>(obj);
|
||||
@@ -821,7 +829,8 @@ inline bool VerifyCharacter(::flatbuffers::Verifier &verifier, const void *obj,
|
||||
}
|
||||
}
|
||||
|
||||
inline bool VerifyCharacterVector(::flatbuffers::Verifier &verifier, const ::flatbuffers::Vector<::flatbuffers::Offset<void>> *values, const ::flatbuffers::Vector<Character> *types) {
|
||||
template <bool B>
|
||||
inline bool VerifyCharacterVector(::flatbuffers::VerifierTemplate<B> &verifier, const ::flatbuffers::Vector<::flatbuffers::Offset<void>> *values, const ::flatbuffers::Vector<Character> *types) {
|
||||
if (!values || !types) return !values && !types;
|
||||
if (values->size() != types->size()) return false;
|
||||
for (::flatbuffers::uoffset_t i = 0; i < values->size(); ++i) {
|
||||
@@ -964,13 +973,14 @@ inline void CharacterUnion::Reset() {
|
||||
type = Character::NONE;
|
||||
}
|
||||
|
||||
inline bool VerifyGadget(::flatbuffers::Verifier &verifier, const void *obj, Gadget type) {
|
||||
template <bool B>
|
||||
inline bool VerifyGadget(::flatbuffers::VerifierTemplate<B> &verifier, const void *obj, Gadget type) {
|
||||
switch (type) {
|
||||
case Gadget::NONE: {
|
||||
return true;
|
||||
}
|
||||
case Gadget::FallingTub: {
|
||||
return verifier.VerifyField<FallingTub>(static_cast<const uint8_t *>(obj), 0, 4);
|
||||
return verifier.template VerifyField<FallingTub>(static_cast<const uint8_t *>(obj), 0, 4);
|
||||
}
|
||||
case Gadget::HandFan: {
|
||||
auto ptr = reinterpret_cast<const HandFan *>(obj);
|
||||
@@ -980,7 +990,8 @@ inline bool VerifyGadget(::flatbuffers::Verifier &verifier, const void *obj, Gad
|
||||
}
|
||||
}
|
||||
|
||||
inline bool VerifyGadgetVector(::flatbuffers::Verifier &verifier, const ::flatbuffers::Vector<::flatbuffers::Offset<void>> *values, const ::flatbuffers::Vector<Gadget> *types) {
|
||||
template <bool B>
|
||||
inline bool VerifyGadgetVector(::flatbuffers::VerifierTemplate<B> &verifier, const ::flatbuffers::Vector<::flatbuffers::Offset<void>> *values, const ::flatbuffers::Vector<Gadget> *types) {
|
||||
if (!values || !types) return !values && !types;
|
||||
if (values->size() != types->size()) return false;
|
||||
for (::flatbuffers::uoffset_t i = 0; i < values->size(); ++i) {
|
||||
@@ -1226,14 +1237,16 @@ inline bool SizePrefixedMovieBufferHasIdentifier(const void *buf) {
|
||||
buf, MovieIdentifier(), true);
|
||||
}
|
||||
|
||||
template <bool B = false>
|
||||
inline bool VerifyMovieBuffer(
|
||||
::flatbuffers::Verifier &verifier) {
|
||||
return verifier.VerifyBuffer<Movie>(MovieIdentifier());
|
||||
::flatbuffers::VerifierTemplate<B> &verifier) {
|
||||
return verifier.template VerifyBuffer<Movie>(MovieIdentifier());
|
||||
}
|
||||
|
||||
template <bool B = false>
|
||||
inline bool VerifySizePrefixedMovieBuffer(
|
||||
::flatbuffers::Verifier &verifier) {
|
||||
return verifier.VerifySizePrefixedBuffer<Movie>(MovieIdentifier());
|
||||
::flatbuffers::VerifierTemplate<B> &verifier) {
|
||||
return verifier.template VerifySizePrefixedBuffer<Movie>(MovieIdentifier());
|
||||
}
|
||||
|
||||
inline void FinishMovieBuffer(
|
||||
|
||||
Reference in New Issue
Block a user