diff --git a/BUILD.bazel b/BUILD.bazel index 7d471436d21..f2aac0ae384 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -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", diff --git a/CMakeLists.txt b/CMakeLists.txt index 95d7a5a955b..df71de82e24 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 diff --git a/android/app/src/main/cpp/flatbuffers/CMakeLists.txt b/android/app/src/main/cpp/flatbuffers/CMakeLists.txt index 0e5f3e94898..e1dd1e8d358 100644 --- a/android/app/src/main/cpp/flatbuffers/CMakeLists.txt +++ b/android/app/src/main/cpp/flatbuffers/CMakeLists.txt @@ -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 diff --git a/include/flatbuffers/flex_flat_util.h b/include/flatbuffers/flex_flat_util.h new file mode 100644 index 00000000000..020957ebe4f --- /dev/null +++ b/include/flatbuffers/flex_flat_util.h @@ -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 *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_ diff --git a/include/flatbuffers/flexbuffers.h b/include/flatbuffers/flexbuffers.h index b4b0332b625..e841368ff57 100644 --- a/include/flatbuffers/flexbuffers.h +++ b/include/flatbuffers/flexbuffers.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 static Type GetScalarType() { static_assert(flatbuffers::is_scalar::value, "Unrelated types"); - return flatbuffers::is_floating_point::value ? FBT_FLOAT - : flatbuffers::is_same::value - ? FBT_BOOL - : (flatbuffers::is_unsigned::value ? FBT_UINT : FBT_INT); + return flatbuffers::is_floating_point::value + ? FBT_FLOAT + : flatbuffers::is_same::value + ? FBT_BOOL + : (flatbuffers::is_unsigned::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 *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) diff --git a/src/idl_gen_cpp.cpp b/src/idl_gen_cpp.cpp index cf6522fac7d..564c802801c 100644 --- a/src/idl_gen_cpp.cpp +++ b/src/idl_gen_cpp.cpp @@ -370,6 +370,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(); diff --git a/tests/cpp17/generated_cpp17/monster_test_generated.h b/tests/cpp17/generated_cpp17/monster_test_generated.h index e802585b0f2..494e72a3e0b 100644 --- a/tests/cpp17/generated_cpp17/monster_test_generated.h +++ b/tests/cpp17/generated_cpp17/monster_test_generated.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. diff --git a/tests/cpp17/test_cpp17.cpp b/tests/cpp17/test_cpp17.cpp index a17205a68ed..0796f9cfac3 100644 --- a/tests/cpp17/test_cpp17.cpp +++ b/tests/cpp17/test_cpp17.cpp @@ -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); +static_assert(std::is_same_v); using mana_type = Monster::Traits::FieldType<1>; static_assert(std::is_same_v); using name_type = Monster::Traits::FieldType<3>; -static_assert(std::is_same_v); +static_assert(std::is_same_v); /******************************************************************************* ** Generic Create Function Test. diff --git a/tests/fuzzer/CMakeLists.txt b/tests/fuzzer/CMakeLists.txt index 1edc69a59e4..63bda687885 100644 --- a/tests/fuzzer/CMakeLists.txt +++ b/tests/fuzzer/CMakeLists.txt @@ -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 diff --git a/tests/monster_test_generated.h b/tests/monster_test_generated.h index 3b36ccc1ebc..5e2308c7bfe 100644 --- a/tests/monster_test_generated.h +++ b/tests/monster_test_generated.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.