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:
Derek Bailey
2022-04-05 16:29:42 -07:00
committed by GitHub
parent bf17df346e
commit f083b33f2a
10 changed files with 54 additions and 19 deletions

View 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_

View File

@@ -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)