@@ -371,10 +371,7 @@ void AppendToString(std::string &s, T &&v, bool keys_quoted) {
371371class Reference {
372372 public:
373373 Reference ()
374- : data_(nullptr ),
375- parent_width_ (0 ),
376- byte_width_(0 ),
377- type_(FBT_NULL) {}
374+ : data_(nullptr ), parent_width_(0 ), byte_width_(0 ), type_(FBT_NULL) {}
378375
379376 Reference (const uint8_t *data, uint8_t parent_width, uint8_t byte_width,
380377 Type type)
@@ -1645,8 +1642,7 @@ class Verifier FLATBUFFERS_FINAL_CLASS {
16451642 // comes at the cost of using additional memory the same size of
16461643 // the buffer being verified, so it is by default off.
16471644 std::vector<uint8_t > *reuse_tracker = nullptr ,
1648- bool _check_alignment = true ,
1649- size_t max_depth = 64 )
1645+ bool _check_alignment = true , size_t max_depth = 64 )
16501646 : buf_(buf),
16511647 size_ (buf_len),
16521648 depth_(0 ),
@@ -1689,35 +1685,33 @@ class Verifier FLATBUFFERS_FINAL_CLASS {
16891685 auto o = static_cast <size_t >(p - buf_);
16901686 return VerifyBefore (o, len);
16911687 }
1692-
1688+
16931689 bool VerifyByteWidth (size_t width) {
16941690 return Check (width == 1 || width == 2 || width == 4 || width == 8 );
16951691 }
16961692
1697- bool VerifyType (int type) {
1698- return Check (type >= 0 && type < FBT_MAX_TYPE);
1699- }
1693+ bool VerifyType (int type) { return Check (type >= 0 && type < FBT_MAX_TYPE); }
17001694
17011695 bool VerifyOffset (uint64_t off, const uint8_t *p) {
17021696 return Check (off <= static_cast <uint64_t >(size_)) &&
1703- off <= static_cast <uint64_t >(p - buf_);
1697+ off <= static_cast <uint64_t >(p - buf_);
17041698 }
17051699
17061700 bool VerifyAlignment (const uint8_t *p, size_t size) const {
17071701 auto o = static_cast <size_t >(p - buf_);
17081702 return Check ((o & (size - 1 )) == 0 || !check_alignment_);
17091703 }
17101704
1711- // Macro, since we want to escape from parent function & use lazy args.
1712- #define FLEX_CHECK_VERIFIED (P, PACKED_TYPE ) \
1713- if (reuse_tracker_) { \
1714- auto packed_type = PACKED_TYPE; \
1715- auto existing = (*reuse_tracker_)[P - buf_]; \
1716- if (existing == packed_type) return true ; \
1717- /* Fail verification if already set with different type! */ \
1718- if (!Check (existing == 0 )) return false ; \
1719- (*reuse_tracker_)[P - buf_] = packed_type; \
1720- }
1705+ // Macro, since we want to escape from parent function & use lazy args.
1706+ #define FLEX_CHECK_VERIFIED (P, PACKED_TYPE ) \
1707+ if (reuse_tracker_) { \
1708+ auto packed_type = PACKED_TYPE; \
1709+ auto existing = (*reuse_tracker_)[P - buf_]; \
1710+ if (existing == packed_type) return true ; \
1711+ /* Fail verification if already set with different type! */ \
1712+ if (!Check (existing == 0 )) return false ; \
1713+ (*reuse_tracker_)[P - buf_] = packed_type; \
1714+ }
17211715
17221716 bool VerifyVector (Reference r, const uint8_t *p, Type elem_type) {
17231717 // Any kind of nesting goes thru this function, so guard against that
@@ -1727,19 +1721,19 @@ class Verifier FLATBUFFERS_FINAL_CLASS {
17271721 if (!Check (depth_ <= max_depth_ && num_vectors_ <= max_vectors_))
17281722 return false ;
17291723 auto size_byte_width = r.byte_width_ ;
1730- FLEX_CHECK_VERIFIED (p, PackedType ( Builder::WidthB (size_byte_width), r. type_ ));
1731- if (! VerifyBeforePointer (p, size_byte_width))
1732- return false ;
1724+ FLEX_CHECK_VERIFIED (p,
1725+ PackedType ( Builder::WidthB (size_byte_width), r. type_ ));
1726+ if (! VerifyBeforePointer (p, size_byte_width)) return false ;
17331727 auto sized = Sized (p, size_byte_width);
17341728 auto num_elems = sized.size ();
1735- auto elem_byte_width =
1736- r.type_ == FBT_STRING || r.type_ == FBT_BLOB ? uint8_t (1 ) : r.byte_width_ ;
1729+ auto elem_byte_width = r.type_ == FBT_STRING || r.type_ == FBT_BLOB
1730+ ? uint8_t (1 )
1731+ : r.byte_width_ ;
17371732 auto max_elems = SIZE_MAX / elem_byte_width;
17381733 if (!Check (num_elems < max_elems))
17391734 return false ; // Protect against byte_size overflowing.
17401735 auto byte_size = num_elems * elem_byte_width;
1741- if (!VerifyFromPointer (p, byte_size))
1742- return false ;
1736+ if (!VerifyFromPointer (p, byte_size)) return false ;
17431737 if (elem_type == FBT_NULL) {
17441738 // Verify type bytes after the vector.
17451739 if (!VerifyFromPointer (p + byte_size, num_elems)) return false ;
@@ -1760,28 +1754,25 @@ class Verifier FLATBUFFERS_FINAL_CLASS {
17601754 bool VerifyKeys (const uint8_t *p, uint8_t byte_width) {
17611755 // The vector part of the map has already been verified.
17621756 const size_t num_prefixed_fields = 3 ;
1763- if (!VerifyBeforePointer (p, byte_width * num_prefixed_fields))
1764- return false ;
1757+ if (!VerifyBeforePointer (p, byte_width * num_prefixed_fields)) return false ;
17651758 p -= byte_width * num_prefixed_fields;
17661759 auto off = ReadUInt64 (p, byte_width);
1767- if (!VerifyOffset (off, p))
1768- return false ;
1760+ if (!VerifyOffset (off, p)) return false ;
17691761 auto key_byte_with =
1770- static_cast <uint8_t >(ReadUInt64 (p + byte_width, byte_width));
1771- if (!VerifyByteWidth (key_byte_with))
1772- return false ;
1762+ static_cast <uint8_t >(ReadUInt64 (p + byte_width, byte_width));
1763+ if (!VerifyByteWidth (key_byte_with)) return false ;
17731764 return VerifyVector (Reference (p, byte_width, key_byte_with, FBT_VECTOR_KEY),
17741765 p - off, FBT_KEY);
17751766 }
17761767
1777- bool VerifyKey (const uint8_t * p) {
1768+ bool VerifyKey (const uint8_t * p) {
17781769 FLEX_CHECK_VERIFIED (p, PackedType (BIT_WIDTH_8, FBT_KEY));
17791770 while (p < buf_ + size_)
17801771 if (*p++) return true ;
17811772 return false ;
17821773 }
17831774
1784- #undef FLEX_CHECK_VERIFIED
1775+ #undef FLEX_CHECK_VERIFIED
17851776
17861777 bool VerifyTerminator (const String &s) {
17871778 return VerifyFromPointer (reinterpret_cast <const uint8_t *>(s.c_str ()),
@@ -1799,37 +1790,26 @@ class Verifier FLATBUFFERS_FINAL_CLASS {
17991790 }
18001791 // All remaining types are an offset.
18011792 auto off = ReadUInt64 (r.data_ , r.parent_width_ );
1802- if (!VerifyOffset (off, r.data_ ))
1803- return false ;
1793+ if (!VerifyOffset (off, r.data_ )) return false ;
18041794 auto p = r.Indirect ();
1805- if (!VerifyAlignment (p, r.byte_width_ ))
1806- return false ;
1795+ if (!VerifyAlignment (p, r.byte_width_ )) return false ;
18071796 switch (r.type_ ) {
18081797 case FBT_INDIRECT_INT:
18091798 case FBT_INDIRECT_UINT:
1810- case FBT_INDIRECT_FLOAT:
1811- return VerifyFromPointer (p, r.byte_width_ );
1812- case FBT_KEY:
1813- return VerifyKey (p);
1799+ case FBT_INDIRECT_FLOAT: return VerifyFromPointer (p, r.byte_width_ );
1800+ case FBT_KEY: return VerifyKey (p);
18141801 case FBT_MAP:
1815- return VerifyVector (r, p, FBT_NULL) &&
1816- VerifyKeys (p, r.byte_width_ );
1817- case FBT_VECTOR:
1818- return VerifyVector (r, p, FBT_NULL);
1819- case FBT_VECTOR_INT:
1820- return VerifyVector (r, p, FBT_INT);
1802+ return VerifyVector (r, p, FBT_NULL) && VerifyKeys (p, r.byte_width_ );
1803+ case FBT_VECTOR: return VerifyVector (r, p, FBT_NULL);
1804+ case FBT_VECTOR_INT: return VerifyVector (r, p, FBT_INT);
18211805 case FBT_VECTOR_BOOL:
1822- case FBT_VECTOR_UINT:
1823- return VerifyVector (r, p, FBT_UINT);
1824- case FBT_VECTOR_FLOAT:
1825- return VerifyVector (r, p, FBT_FLOAT);
1826- case FBT_VECTOR_KEY:
1827- return VerifyVector (r, p, FBT_KEY);
1806+ case FBT_VECTOR_UINT: return VerifyVector (r, p, FBT_UINT);
1807+ case FBT_VECTOR_FLOAT: return VerifyVector (r, p, FBT_FLOAT);
1808+ case FBT_VECTOR_KEY: return VerifyVector (r, p, FBT_KEY);
18281809 case FBT_VECTOR_STRING_DEPRECATED:
18291810 // Use of FBT_KEY here intentional, see elsewhere.
18301811 return VerifyVector (r, p, FBT_KEY);
1831- case FBT_BLOB:
1832- return VerifyVector (r, p, FBT_UINT);
1812+ case FBT_BLOB: return VerifyVector (r, p, FBT_UINT);
18331813 case FBT_STRING:
18341814 return VerifyVector (r, p, FBT_UINT) &&
18351815 VerifyTerminator (String (p, r.byte_width_ ));
@@ -1844,12 +1824,10 @@ class Verifier FLATBUFFERS_FINAL_CLASS {
18441824 case FBT_VECTOR_FLOAT4: {
18451825 uint8_t len = 0 ;
18461826 auto vtype = ToFixedTypedVectorElementType (r.type_ , &len);
1847- if (!VerifyType (vtype))
1848- return false ;
1827+ if (!VerifyType (vtype)) return false ;
18491828 return VerifyFromPointer (p, r.byte_width_ * len);
18501829 }
1851- default :
1852- return false ;
1830+ default : return false ;
18531831 }
18541832 }
18551833
@@ -1859,8 +1837,7 @@ class Verifier FLATBUFFERS_FINAL_CLASS {
18591837 auto end = buf_ + size_;
18601838 auto byte_width = *--end;
18611839 auto packed_type = *--end;
1862- return VerifyByteWidth (byte_width) &&
1863- Check (end - buf_ >= byte_width) &&
1840+ return VerifyByteWidth (byte_width) && Check (end - buf_ >= byte_width) &&
18641841 VerifyRef (Reference (end - byte_width, byte_width, packed_type));
18651842 }
18661843
@@ -1875,24 +1852,23 @@ class Verifier FLATBUFFERS_FINAL_CLASS {
18751852 std::vector<uint8_t > *reuse_tracker_;
18761853};
18771854
1878- // Utility function that contructs the Verifier for you, see above for parameters.
1855+ // Utility function that contructs the Verifier for you, see above for
1856+ // parameters.
18791857inline bool VerifyBuffer (const uint8_t *buf, size_t buf_len,
18801858 std::vector<uint8_t > *reuse_tracker = nullptr ) {
18811859 Verifier verifier (buf, buf_len, reuse_tracker);
18821860 return verifier.VerifyBuffer ();
18831861}
18841862
1885-
18861863#ifdef FLATBUFFERS_H_
18871864// This is a verifier utility function that works together with the
18881865// FlatBuffers verifier, which should only be present if flatbuffer.h
18891866// has been included (which it typically is in generated code).
18901867inline bool VerifyNestedFlexBuffer (const flatbuffers::Vector<uint8_t > *nv,
18911868 flatbuffers::Verifier &verifier) {
18921869 if (!nv) return true ;
1893- return verifier.Check (
1894- flexbuffers::VerifyBuffer (nv->data (), nv->size (),
1895- verifier.GetFlexReuseTracker ()));
1870+ return verifier.Check (flexbuffers::VerifyBuffer (
1871+ nv->data (), nv->size (), verifier.GetFlexReuseTracker ()));
18961872}
18971873#endif
18981874
0 commit comments