mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-15 16:57:29 +00:00
code gen flexbuffer verifier (#7207)
* code gen flexbuffer verifier * remove verify nested flexbuffers from flexbuffers * made function static, and placed higher in file * moved function to own header
This commit is contained in:
@@ -49,6 +49,7 @@ filegroup(
|
||||
"include/flatbuffers/detached_buffer.h",
|
||||
"include/flatbuffers/flatbuffer_builder.h",
|
||||
"include/flatbuffers/flatbuffers.h",
|
||||
"include/flatbuffers/flex_flat_util.h",
|
||||
"include/flatbuffers/flexbuffers.h",
|
||||
"include/flatbuffers/grpc.h",
|
||||
"include/flatbuffers/hash.h",
|
||||
|
||||
@@ -120,6 +120,7 @@ set(FlatBuffers_Library_SRCS
|
||||
include/flatbuffers/flatbuffer_builder.h
|
||||
include/flatbuffers/flatbuffers.h
|
||||
include/flatbuffers/flexbuffers.h
|
||||
include/flatbuffers/flex_flat_util.h
|
||||
include/flatbuffers/hash.h
|
||||
include/flatbuffers/idl.h
|
||||
include/flatbuffers/minireflect.h
|
||||
|
||||
@@ -26,6 +26,7 @@ set(FlatBuffers_Library_SRCS
|
||||
${FLATBUFFERS_SRC}/include/flatbuffers/flatbuffer_builder.h
|
||||
${FLATBUFFERS_SRC}/include/flatbuffers/flatbuffers.h
|
||||
${FLATBUFFERS_SRC}/include/flatbuffers/flexbuffers.h
|
||||
${FLATBUFFERS_SRC}/include/flatbuffers/flex_flat_util.h
|
||||
${FLATBUFFERS_SRC}/include/flatbuffers/hash.h
|
||||
${FLATBUFFERS_SRC}/include/flatbuffers/idl.h
|
||||
${FLATBUFFERS_SRC}/include/flatbuffers/minireflect.h
|
||||
|
||||
36
include/flatbuffers/flex_flat_util.h
Normal file
36
include/flatbuffers/flex_flat_util.h
Normal file
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* Copyright 2022 Google Inc. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef FLATBUFFERS_FLEX_FLAT_UTIL_H_
|
||||
#define FLATBUFFERS_FLEX_FLAT_UTIL_H_
|
||||
|
||||
#include "flatbuffers/flatbuffers.h"
|
||||
#include "flatbuffers/flexbuffers.h"
|
||||
|
||||
namespace flexbuffers {
|
||||
|
||||
// Verifies the `nested` flexbuffer within a flatbuffer vector is valid.
|
||||
inline bool VerifyNestedFlexBuffer(
|
||||
const flatbuffers::Vector<uint8_t> *const nested,
|
||||
flatbuffers::Verifier &verifier) {
|
||||
if (!nested) return true;
|
||||
return verifier.Check(flexbuffers::VerifyBuffer(
|
||||
nested->data(), nested->size(), verifier.GetFlexReuseTracker()));
|
||||
}
|
||||
|
||||
} // namespace flexbuffers
|
||||
|
||||
#endif // FLATBUFFERS_FLEX_FLAT_UTIL_H_
|
||||
@@ -156,6 +156,7 @@ inline uint64_t ReadUInt64(const uint8_t *data, uint8_t byte_width) {
|
||||
// TODO: GCC apparently replaces memcpy by a rep movsb, but only if count is a
|
||||
// constant, which here it isn't. Test if memcpy is still faster than
|
||||
// the conditionals in ReadSizedScalar. Can also use inline asm.
|
||||
|
||||
// clang-format off
|
||||
#if defined(_MSC_VER) && defined(_M_X64) && !defined(_M_ARM64EC)
|
||||
// This is 64-bit Windows only, __movsb does not work on 32-bit Windows.
|
||||
@@ -578,7 +579,7 @@ class Reference {
|
||||
// unquoted if it looks like an "identifier":
|
||||
const char *p = keys[i].AsKey();
|
||||
if (!flatbuffers::is_alpha(*p) && *p != '_') {
|
||||
kq = true;
|
||||
kq = true;
|
||||
} else {
|
||||
while (*++p) {
|
||||
if (!flatbuffers::is_alnum(*p) && *p != '_') {
|
||||
@@ -1422,10 +1423,12 @@ class Builder FLATBUFFERS_FINAL_CLASS {
|
||||
|
||||
template<typename T> static Type GetScalarType() {
|
||||
static_assert(flatbuffers::is_scalar<T>::value, "Unrelated types");
|
||||
return flatbuffers::is_floating_point<T>::value ? FBT_FLOAT
|
||||
: flatbuffers::is_same<T, bool>::value
|
||||
? FBT_BOOL
|
||||
: (flatbuffers::is_unsigned<T>::value ? FBT_UINT : FBT_INT);
|
||||
return flatbuffers::is_floating_point<T>::value
|
||||
? FBT_FLOAT
|
||||
: flatbuffers::is_same<T, bool>::value
|
||||
? FBT_BOOL
|
||||
: (flatbuffers::is_unsigned<T>::value ? FBT_UINT
|
||||
: FBT_INT);
|
||||
}
|
||||
|
||||
public:
|
||||
@@ -1876,18 +1879,6 @@ inline bool VerifyBuffer(const uint8_t *buf, size_t buf_len,
|
||||
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
|
||||
// has been included (which it typically is in generated code).
|
||||
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()));
|
||||
}
|
||||
#endif
|
||||
|
||||
} // namespace flexbuffers
|
||||
|
||||
#if defined(_MSC_VER)
|
||||
|
||||
@@ -377,6 +377,7 @@ class CppGenerator : public BaseGenerator {
|
||||
code_ += "#include \"flatbuffers/flatbuffers.h\"";
|
||||
if (parser_.uses_flexbuffers_) {
|
||||
code_ += "#include \"flatbuffers/flexbuffers.h\"";
|
||||
code_ += "#include \"flatbuffers/flex_flat_util.h\"";
|
||||
}
|
||||
code_ += "";
|
||||
GenFlatbuffersVersionCheck();
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
|
||||
#include "flatbuffers/flatbuffers.h"
|
||||
#include "flatbuffers/flexbuffers.h"
|
||||
#include "flatbuffers/flex_flat_util.h"
|
||||
|
||||
// Ensure the included flatbuffers.h is the same version as when this file was
|
||||
// generated, otherwise it may not be compatible.
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
// This is an experimental feature and could change at any time.
|
||||
|
||||
#include "flatbuffers/flatbuffers.h"
|
||||
#include "flatbuffers/flex_flat_util.h"
|
||||
#include "flatbuffers/flexbuffers.h"
|
||||
#include "flatbuffers/idl.h"
|
||||
#include "flatbuffers/minireflect.h"
|
||||
@@ -167,13 +168,13 @@ void StringifyAnyFlatbuffersTypeTest() {
|
||||
** Test Traits::FieldType
|
||||
*******************************************************************************/
|
||||
using pos_type = Monster::Traits::FieldType<0>;
|
||||
static_assert(std::is_same_v<pos_type, const Vec3*>);
|
||||
static_assert(std::is_same_v<pos_type, const Vec3 *>);
|
||||
|
||||
using mana_type = Monster::Traits::FieldType<1>;
|
||||
static_assert(std::is_same_v<mana_type, int16_t>);
|
||||
|
||||
using name_type = Monster::Traits::FieldType<3>;
|
||||
static_assert(std::is_same_v<name_type, const flatbuffers::String*>);
|
||||
static_assert(std::is_same_v<name_type, const flatbuffers::String *>);
|
||||
|
||||
/*******************************************************************************
|
||||
** Generic Create Function Test.
|
||||
|
||||
@@ -95,6 +95,7 @@ set(FlatBuffers_Library_SRCS
|
||||
${FLATBUFFERS_DIR}/include/flatbuffers/flatbuffer_builder.h
|
||||
${FLATBUFFERS_DIR}/include/flatbuffers/flatbuffers.h
|
||||
${FLATBUFFERS_DIR}/include/flatbuffers/flexbuffers.h
|
||||
${FLATBUFFERS_DIR}/include/flatbuffers/flex_flat_util.h
|
||||
${FLATBUFFERS_DIR}/include/flatbuffers/hash.h
|
||||
${FLATBUFFERS_DIR}/include/flatbuffers/idl.h
|
||||
${FLATBUFFERS_DIR}/include/flatbuffers/minireflect.h
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
|
||||
#include "flatbuffers/flatbuffers.h"
|
||||
#include "flatbuffers/flexbuffers.h"
|
||||
#include "flatbuffers/flex_flat_util.h"
|
||||
|
||||
// Ensure the included flatbuffers.h is the same version as when this file was
|
||||
// generated, otherwise it may not be compatible.
|
||||
|
||||
Reference in New Issue
Block a user