mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-07 22:03:40 +00:00
clang format on codebase (#7058)
This commit is contained in:
@@ -285,9 +285,7 @@ class FlatBufferBuilder {
|
||||
FieldLoc fl = { off, field };
|
||||
buf_.scratch_push_small(fl);
|
||||
num_field_loc++;
|
||||
if (field > max_voffset_) {
|
||||
max_voffset_ = field;
|
||||
}
|
||||
if (field > max_voffset_) { max_voffset_ = field; }
|
||||
}
|
||||
|
||||
// Like PushElement, but additionally tracks the field this represents.
|
||||
@@ -1074,7 +1072,7 @@ class FlatBufferBuilder {
|
||||
void SwapBufAllocator(FlatBufferBuilder &other) {
|
||||
buf_.swap_allocator(other.buf_);
|
||||
}
|
||||
|
||||
|
||||
/// @brief The length of a FlatBuffer file header.
|
||||
static const size_t kFileIdentifierLength =
|
||||
::flatbuffers::kFileIdentifierLength;
|
||||
|
||||
@@ -226,12 +226,13 @@ struct TypeTable {
|
||||
};
|
||||
|
||||
// String which identifies the current version of FlatBuffers.
|
||||
inline const char * flatbuffers_version_string() {
|
||||
inline const char *flatbuffers_version_string() {
|
||||
return "FlatBuffers " FLATBUFFERS_STRING(FLATBUFFERS_VERSION_MAJOR) "."
|
||||
FLATBUFFERS_STRING(FLATBUFFERS_VERSION_MINOR) "."
|
||||
FLATBUFFERS_STRING(FLATBUFFERS_VERSION_REVISION);
|
||||
}
|
||||
|
||||
// clang-format off
|
||||
#define FLATBUFFERS_DEFINE_BITMASK_OPERATORS(E, T)\
|
||||
inline E operator | (E lhs, E rhs){\
|
||||
return E(T(lhs) | T(rhs));\
|
||||
|
||||
@@ -371,10 +371,7 @@ void AppendToString(std::string &s, T &&v, bool keys_quoted) {
|
||||
class Reference {
|
||||
public:
|
||||
Reference()
|
||||
: data_(nullptr),
|
||||
parent_width_(0),
|
||||
byte_width_(0),
|
||||
type_(FBT_NULL) {}
|
||||
: data_(nullptr), parent_width_(0), byte_width_(0), type_(FBT_NULL) {}
|
||||
|
||||
Reference(const uint8_t *data, uint8_t parent_width, uint8_t byte_width,
|
||||
Type type)
|
||||
@@ -1645,8 +1642,7 @@ class Verifier FLATBUFFERS_FINAL_CLASS {
|
||||
// comes at the cost of using additional memory the same size of
|
||||
// the buffer being verified, so it is by default off.
|
||||
std::vector<uint8_t> *reuse_tracker = nullptr,
|
||||
bool _check_alignment = true,
|
||||
size_t max_depth = 64)
|
||||
bool _check_alignment = true, size_t max_depth = 64)
|
||||
: buf_(buf),
|
||||
size_(buf_len),
|
||||
depth_(0),
|
||||
@@ -1689,18 +1685,16 @@ class Verifier FLATBUFFERS_FINAL_CLASS {
|
||||
auto o = static_cast<size_t>(p - buf_);
|
||||
return VerifyBefore(o, len);
|
||||
}
|
||||
|
||||
|
||||
bool VerifyByteWidth(size_t width) {
|
||||
return Check(width == 1 || width == 2 || width == 4 || width == 8);
|
||||
}
|
||||
|
||||
bool VerifyType(int type) {
|
||||
return Check(type >= 0 && type < FBT_MAX_TYPE);
|
||||
}
|
||||
bool VerifyType(int type) { return Check(type >= 0 && type < FBT_MAX_TYPE); }
|
||||
|
||||
bool VerifyOffset(uint64_t off, const uint8_t *p) {
|
||||
return Check(off <= static_cast<uint64_t>(size_)) &&
|
||||
off <= static_cast<uint64_t>(p - buf_);
|
||||
off <= static_cast<uint64_t>(p - buf_);
|
||||
}
|
||||
|
||||
bool VerifyAlignment(const uint8_t *p, size_t size) const {
|
||||
@@ -1708,16 +1702,16 @@ class Verifier FLATBUFFERS_FINAL_CLASS {
|
||||
return Check((o & (size - 1)) == 0 || !check_alignment_);
|
||||
}
|
||||
|
||||
// Macro, since we want to escape from parent function & use lazy args.
|
||||
#define FLEX_CHECK_VERIFIED(P, PACKED_TYPE) \
|
||||
if (reuse_tracker_) { \
|
||||
auto packed_type = PACKED_TYPE; \
|
||||
auto existing = (*reuse_tracker_)[P - buf_]; \
|
||||
if (existing == packed_type) return true; \
|
||||
/* Fail verification if already set with different type! */ \
|
||||
if (!Check(existing == 0)) return false; \
|
||||
(*reuse_tracker_)[P - buf_] = packed_type; \
|
||||
}
|
||||
// Macro, since we want to escape from parent function & use lazy args.
|
||||
#define FLEX_CHECK_VERIFIED(P, PACKED_TYPE) \
|
||||
if (reuse_tracker_) { \
|
||||
auto packed_type = PACKED_TYPE; \
|
||||
auto existing = (*reuse_tracker_)[P - buf_]; \
|
||||
if (existing == packed_type) return true; \
|
||||
/* Fail verification if already set with different type! */ \
|
||||
if (!Check(existing == 0)) return false; \
|
||||
(*reuse_tracker_)[P - buf_] = packed_type; \
|
||||
}
|
||||
|
||||
bool VerifyVector(Reference r, const uint8_t *p, Type elem_type) {
|
||||
// Any kind of nesting goes thru this function, so guard against that
|
||||
@@ -1727,19 +1721,19 @@ class Verifier FLATBUFFERS_FINAL_CLASS {
|
||||
if (!Check(depth_ <= max_depth_ && num_vectors_ <= max_vectors_))
|
||||
return false;
|
||||
auto size_byte_width = r.byte_width_;
|
||||
FLEX_CHECK_VERIFIED(p, PackedType(Builder::WidthB(size_byte_width), r.type_));
|
||||
if (!VerifyBeforePointer(p, size_byte_width))
|
||||
return false;
|
||||
FLEX_CHECK_VERIFIED(p,
|
||||
PackedType(Builder::WidthB(size_byte_width), r.type_));
|
||||
if (!VerifyBeforePointer(p, size_byte_width)) return false;
|
||||
auto sized = Sized(p, size_byte_width);
|
||||
auto num_elems = sized.size();
|
||||
auto elem_byte_width =
|
||||
r.type_ == FBT_STRING || r.type_ == FBT_BLOB ? uint8_t(1) : r.byte_width_;
|
||||
auto elem_byte_width = r.type_ == FBT_STRING || r.type_ == FBT_BLOB
|
||||
? uint8_t(1)
|
||||
: r.byte_width_;
|
||||
auto max_elems = SIZE_MAX / elem_byte_width;
|
||||
if (!Check(num_elems < max_elems))
|
||||
return false; // Protect against byte_size overflowing.
|
||||
auto byte_size = num_elems * elem_byte_width;
|
||||
if (!VerifyFromPointer(p, byte_size))
|
||||
return false;
|
||||
if (!VerifyFromPointer(p, byte_size)) return false;
|
||||
if (elem_type == FBT_NULL) {
|
||||
// Verify type bytes after the vector.
|
||||
if (!VerifyFromPointer(p + byte_size, num_elems)) return false;
|
||||
@@ -1760,28 +1754,25 @@ class Verifier FLATBUFFERS_FINAL_CLASS {
|
||||
bool VerifyKeys(const uint8_t *p, uint8_t byte_width) {
|
||||
// The vector part of the map has already been verified.
|
||||
const size_t num_prefixed_fields = 3;
|
||||
if (!VerifyBeforePointer(p, byte_width * num_prefixed_fields))
|
||||
return false;
|
||||
if (!VerifyBeforePointer(p, byte_width * num_prefixed_fields)) return false;
|
||||
p -= byte_width * num_prefixed_fields;
|
||||
auto off = ReadUInt64(p, byte_width);
|
||||
if (!VerifyOffset(off, p))
|
||||
return false;
|
||||
if (!VerifyOffset(off, p)) return false;
|
||||
auto key_byte_with =
|
||||
static_cast<uint8_t>(ReadUInt64(p + byte_width, byte_width));
|
||||
if (!VerifyByteWidth(key_byte_with))
|
||||
return false;
|
||||
static_cast<uint8_t>(ReadUInt64(p + byte_width, byte_width));
|
||||
if (!VerifyByteWidth(key_byte_with)) return false;
|
||||
return VerifyVector(Reference(p, byte_width, key_byte_with, FBT_VECTOR_KEY),
|
||||
p - off, FBT_KEY);
|
||||
}
|
||||
|
||||
bool VerifyKey(const uint8_t* p) {
|
||||
bool VerifyKey(const uint8_t *p) {
|
||||
FLEX_CHECK_VERIFIED(p, PackedType(BIT_WIDTH_8, FBT_KEY));
|
||||
while (p < buf_ + size_)
|
||||
if (*p++) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
#undef FLEX_CHECK_VERIFIED
|
||||
#undef FLEX_CHECK_VERIFIED
|
||||
|
||||
bool VerifyTerminator(const String &s) {
|
||||
return VerifyFromPointer(reinterpret_cast<const uint8_t *>(s.c_str()),
|
||||
@@ -1799,37 +1790,26 @@ class Verifier FLATBUFFERS_FINAL_CLASS {
|
||||
}
|
||||
// All remaining types are an offset.
|
||||
auto off = ReadUInt64(r.data_, r.parent_width_);
|
||||
if (!VerifyOffset(off, r.data_))
|
||||
return false;
|
||||
if (!VerifyOffset(off, r.data_)) return false;
|
||||
auto p = r.Indirect();
|
||||
if (!VerifyAlignment(p, r.byte_width_))
|
||||
return false;
|
||||
if (!VerifyAlignment(p, r.byte_width_)) return false;
|
||||
switch (r.type_) {
|
||||
case FBT_INDIRECT_INT:
|
||||
case FBT_INDIRECT_UINT:
|
||||
case FBT_INDIRECT_FLOAT:
|
||||
return VerifyFromPointer(p, r.byte_width_);
|
||||
case FBT_KEY:
|
||||
return VerifyKey(p);
|
||||
case FBT_INDIRECT_FLOAT: return VerifyFromPointer(p, r.byte_width_);
|
||||
case FBT_KEY: return VerifyKey(p);
|
||||
case FBT_MAP:
|
||||
return VerifyVector(r, p, FBT_NULL) &&
|
||||
VerifyKeys(p, r.byte_width_);
|
||||
case FBT_VECTOR:
|
||||
return VerifyVector(r, p, FBT_NULL);
|
||||
case FBT_VECTOR_INT:
|
||||
return VerifyVector(r, p, FBT_INT);
|
||||
return VerifyVector(r, p, FBT_NULL) && VerifyKeys(p, r.byte_width_);
|
||||
case FBT_VECTOR: return VerifyVector(r, p, FBT_NULL);
|
||||
case FBT_VECTOR_INT: return VerifyVector(r, p, FBT_INT);
|
||||
case FBT_VECTOR_BOOL:
|
||||
case FBT_VECTOR_UINT:
|
||||
return VerifyVector(r, p, FBT_UINT);
|
||||
case FBT_VECTOR_FLOAT:
|
||||
return VerifyVector(r, p, FBT_FLOAT);
|
||||
case FBT_VECTOR_KEY:
|
||||
return VerifyVector(r, p, FBT_KEY);
|
||||
case FBT_VECTOR_UINT: return VerifyVector(r, p, FBT_UINT);
|
||||
case FBT_VECTOR_FLOAT: return VerifyVector(r, p, FBT_FLOAT);
|
||||
case FBT_VECTOR_KEY: return VerifyVector(r, p, FBT_KEY);
|
||||
case FBT_VECTOR_STRING_DEPRECATED:
|
||||
// Use of FBT_KEY here intentional, see elsewhere.
|
||||
return VerifyVector(r, p, FBT_KEY);
|
||||
case FBT_BLOB:
|
||||
return VerifyVector(r, p, FBT_UINT);
|
||||
case FBT_BLOB: return VerifyVector(r, p, FBT_UINT);
|
||||
case FBT_STRING:
|
||||
return VerifyVector(r, p, FBT_UINT) &&
|
||||
VerifyTerminator(String(p, r.byte_width_));
|
||||
@@ -1844,12 +1824,10 @@ class Verifier FLATBUFFERS_FINAL_CLASS {
|
||||
case FBT_VECTOR_FLOAT4: {
|
||||
uint8_t len = 0;
|
||||
auto vtype = ToFixedTypedVectorElementType(r.type_, &len);
|
||||
if (!VerifyType(vtype))
|
||||
return false;
|
||||
if (!VerifyType(vtype)) return false;
|
||||
return VerifyFromPointer(p, r.byte_width_ * len);
|
||||
}
|
||||
default:
|
||||
return false;
|
||||
default: return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1859,8 +1837,7 @@ class Verifier FLATBUFFERS_FINAL_CLASS {
|
||||
auto end = buf_ + size_;
|
||||
auto byte_width = *--end;
|
||||
auto packed_type = *--end;
|
||||
return VerifyByteWidth(byte_width) &&
|
||||
Check(end - buf_ >= byte_width) &&
|
||||
return VerifyByteWidth(byte_width) && Check(end - buf_ >= byte_width) &&
|
||||
VerifyRef(Reference(end - byte_width, byte_width, packed_type));
|
||||
}
|
||||
|
||||
@@ -1875,14 +1852,14 @@ class Verifier FLATBUFFERS_FINAL_CLASS {
|
||||
std::vector<uint8_t> *reuse_tracker_;
|
||||
};
|
||||
|
||||
// Utility function that contructs the Verifier for you, see above for parameters.
|
||||
// Utility function that contructs the Verifier for you, see above for
|
||||
// parameters.
|
||||
inline bool VerifyBuffer(const uint8_t *buf, size_t buf_len,
|
||||
std::vector<uint8_t> *reuse_tracker = nullptr) {
|
||||
Verifier verifier(buf, buf_len, reuse_tracker);
|
||||
return verifier.VerifyBuffer();
|
||||
}
|
||||
|
||||
|
||||
#ifdef FLATBUFFERS_H_
|
||||
// This is a verifier utility function that works together with the
|
||||
// FlatBuffers verifier, which should only be present if flatbuffer.h
|
||||
@@ -1890,9 +1867,8 @@ inline bool VerifyBuffer(const uint8_t *buf, size_t buf_len,
|
||||
inline bool VerifyNestedFlexBuffer(const flatbuffers::Vector<uint8_t> *nv,
|
||||
flatbuffers::Verifier &verifier) {
|
||||
if (!nv) return true;
|
||||
return verifier.Check(
|
||||
flexbuffers::VerifyBuffer(nv->data(), nv->size(),
|
||||
verifier.GetFlexReuseTracker()));
|
||||
return verifier.Check(flexbuffers::VerifyBuffer(
|
||||
nv->data(), nv->size(), verifier.GetFlexReuseTracker()));
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
@@ -112,18 +112,19 @@ class Table {
|
||||
|
||||
// Verify a particular field.
|
||||
template<typename T>
|
||||
bool VerifyField(const Verifier &verifier, voffset_t field, size_t align) const {
|
||||
bool VerifyField(const Verifier &verifier, voffset_t field,
|
||||
size_t align) const {
|
||||
// Calling GetOptionalFieldOffset should be safe now thanks to
|
||||
// VerifyTable().
|
||||
auto field_offset = GetOptionalFieldOffset(field);
|
||||
// Check the actual field.
|
||||
return !field_offset ||
|
||||
verifier.VerifyField<T>(data_, field_offset, align);
|
||||
return !field_offset || verifier.VerifyField<T>(data_, field_offset, align);
|
||||
}
|
||||
|
||||
// VerifyField for required fields.
|
||||
template<typename T>
|
||||
bool VerifyFieldRequired(const Verifier &verifier, voffset_t field, size_t align) const {
|
||||
bool VerifyFieldRequired(const Verifier &verifier, voffset_t field,
|
||||
size_t align) const {
|
||||
auto field_offset = GetOptionalFieldOffset(field);
|
||||
return verifier.Check(field_offset != 0) &&
|
||||
verifier.VerifyField<T>(data_, field_offset, align);
|
||||
|
||||
@@ -17,15 +17,15 @@
|
||||
#ifndef FLATBUFFERS_UTIL_H_
|
||||
#define FLATBUFFERS_UTIL_H_
|
||||
|
||||
#include <errno.h>
|
||||
#include <ctype.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "flatbuffers/base.h"
|
||||
#include "flatbuffers/stl_emulation.h"
|
||||
|
||||
#ifndef FLATBUFFERS_PREFER_PRINTF
|
||||
# include <sstream>
|
||||
# include <iomanip>
|
||||
# include <sstream>
|
||||
#else // FLATBUFFERS_PREFER_PRINTF
|
||||
# include <float.h>
|
||||
# include <stdio.h>
|
||||
|
||||
@@ -81,14 +81,15 @@ class Verifier FLATBUFFERS_FINAL_CLASS {
|
||||
}
|
||||
|
||||
// Verify relative to a known-good base pointer.
|
||||
bool VerifyFieldStruct(const uint8_t *base, voffset_t elem_off, size_t elem_len,
|
||||
size_t align) const {
|
||||
bool VerifyFieldStruct(const uint8_t *base, voffset_t elem_off,
|
||||
size_t elem_len, size_t align) const {
|
||||
auto f = static_cast<size_t>(base - buf_) + elem_off;
|
||||
return VerifyAlignment(f, align) && Verify(f, elem_len);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool VerifyField(const uint8_t *base, voffset_t elem_off, size_t align) const {
|
||||
bool VerifyField(const uint8_t *base, voffset_t elem_off,
|
||||
size_t align) const {
|
||||
auto f = static_cast<size_t>(base - buf_) + elem_off;
|
||||
return VerifyAlignment(f, align) && Verify(f, sizeof(T));
|
||||
}
|
||||
@@ -259,9 +260,7 @@ class Verifier FLATBUFFERS_FINAL_CLASS {
|
||||
// clang-format on
|
||||
}
|
||||
|
||||
std::vector<uint8_t> *GetFlexReuseTracker() {
|
||||
return flex_reuse_tracker_;
|
||||
}
|
||||
std::vector<uint8_t> *GetFlexReuseTracker() { return flex_reuse_tracker_; }
|
||||
|
||||
void SetFlexReuseTracker(std::vector<uint8_t> *rt) {
|
||||
flex_reuse_tracker_ = rt;
|
||||
|
||||
Reference in New Issue
Block a user