Compare commits

..
Author SHA1 Message Date
Anshul Nigham e38e7aa00c Removing _LIBCPP_DISABLE_UNUSED_STRUCT_WARNINGS from libc++ enables
`[[gnu::__warn_unused__]]` on standard library types (such as
`std::string` and `std::set`), surfacing `-Wunused-variable` warnings
in `//third_party/flatbuffers`. See [upstream LLVM commit](https://github.com/llvm/llvm-project/commit/ab8b037ef7b398aa864959a69f328fa69b567f74) for reference.

Remove unreferenced variable declarations across the code generators and
binary annotator whose initializers have no observable side effects.

PiperOrigin-RevId: 999136370
2026-10-26 16:02:57 -07:00
Omar AbdelsalamandDerek Bailey f58d5768f1 Pin swift-crypto for the Swift CI matrix (#9412)
The Swift test job pulls in grpc-swift-2 -> grpc-swift-nio-transport ->
swift-nio-ssl -> swift-crypto, and this package ships no Package.resolved,
so that whole graph is resolved afresh on every run. On 2026-10-15 it
resolved swift-crypto 5.0.1 (released three days earlier) and "Test Swift
Linux (6.2)" turned red before a single line of FlatBuffers' Swift code was
compiled:

  Sources/Crypto/HPKE/Ciphersuite/HPKE-AEAD.swift:27:6: error: unknown
  attribute 'nonexhaustive'

5.x declares swift-tools-version 6.2, which is why only the 6.2 job selects
it; the 6.0 and 6.1 jobs prune it and keep building. The 6.2.x toolchains
then reject the `@nonexhaustive` that 5.x uses, a combination swift-crypto
does not exercise because it builds on nightly.

Restrict the test-only graph to 4.0.0 ..< 4.4.0, which resolves to 4.3.0:
the last 4.x release that declares swift-tools-version 6.0 and so builds
with every toolchain in the matrix. 4.4+ asks for 6.1 and would break the 6.0
job, and the whole 4.x line is free of `@nonexhaustive`.

Co-authored-by: Derek Bailey <derekbailey@google.com>
2026-10-26 10:11:08 -07:00
Derek Bailey 7af1efe4b2 Regenerate monster_test_bfbs_generated.h with alignas(uint64_t) (#9434)
Commit 6dda43bb (#9425) added `alignas(uint64_t)` to embedded binary schemas
generated by `flatc --bfbs-gen-embed`. Regenerate `tests/monster_test_bfbs_generated.h`
via `scripts/generate_code.py` to keep checked-in test artifacts in sync and
unblock CI Check Generated Code.

GitOrigin-RevId: 3786479bebd33365edc91aa13388cdf95d7313d9
2026-10-26 09:06:45 -07:00
Derek Bailey 6dda43bbaa Ensure embedded FlatBuffers binary schemas are 8-byte aligned (#9425)
In `src/idl_gen_cpp.cpp`, code generated by `flatc --bfbs-gen-embed` emits `static const uint8_t bfbsData[...] = { ... };`. Because `uint8_t` has 1-byte alignment, the array may be placed at an unaligned address in `.rodata`, triggering alignment traps under Clang UBSan on Arm when accessed via FlatBuffers root accessors.

Add `alignas(uint64_t)` to the generated `bfbsData` array declaration in `GenerateBinarySchema` so embedded binary schemas are 8-byte aligned across all architectures.

PiperOrigin-RevId: 1003962636
2026-10-21 13:37:51 -07:00
Derek Bailey 9a8a511b52 Enable locale-independent strtod_l/strtof_l on Linux glibc (#9424)
Restrict the `__linux__` check for `FLATBUFFERS_LOCALE_INDEPENDENT` in `include/flatbuffers/base.h` to `(!defined(__linux__) || defined(__GLIBC__))` so Linux with glibc enables `strtod_l`/`strtof_l`.

PiperOrigin-RevId: 1002266254
2026-10-21 13:25:17 -07:00
Wouter van Oortmerssen b8431fbcd7 Lobster: change builder size constants (enum -> let)
This is because newer versions of the Lobster compiler reject enums with the same values, and older compilers are fine with the let versions also.
2026-09-14 11:02:13 -07:00
carrerasdarren-cellandDarren Carreras 5761d6e67a [Swift] Verify size-prefixed roots from reader position (#9196)
* [Swift] Verify size-prefixed roots from reader position

Validate the size prefix before advancing ByteBuffer.reader, then use the active reader position consistently for root, file identifier, and returned-object verification. Cover malformed decoy roots, oversized prefixes, short identifiers, and valid prefixed identifiers.

* Use wrapping arithmetic in Swift verifier

---------

Co-authored-by: Darren Carreras <283775510+carrerasdarren-cell@users.noreply.github.com>
2026-08-11 21:49:44 +02:00
19 changed files with 154 additions and 81 deletions
+28 -28
View File
@@ -30,7 +30,7 @@ jobs:
cxx: [g++-13, clang++-18]
fail-fast: false
steps:
- uses: actions/checkout@v7
- uses: actions/checkout@v6
- name: cmake
run: CXX=${{ matrix.cxx }} cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Release -DFLATBUFFERS_STRICT_MODE=ON -DFLATBUFFERS_STATIC_FLATC=ON .
- name: build
@@ -68,7 +68,7 @@ jobs:
name: Build Linux with -DFLATBUFFERS_NO_FILE_TESTS
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v7
- uses: actions/checkout@v6
- name: cmake
run: CXX=clang++-18 cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Release -DFLATBUFFERS_STRICT_MODE=ON -DFLATBUFFERS_CXX_FLAGS="-DFLATBUFFERS_NO_FILE_TESTS" .
- name: build
@@ -80,7 +80,7 @@ jobs:
name: Build Linux with out-of-source build location
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v7
- uses: actions/checkout@v6
- name: make build directory
run: mkdir build
- name: cmake
@@ -112,7 +112,7 @@ jobs:
std: 23
steps:
- uses: actions/checkout@v7
- uses: actions/checkout@v6
- name: cmake
run: >
CXX=${{ matrix.cxx }} cmake -G "Unix Makefiles"
@@ -135,7 +135,7 @@ jobs:
std: [11, 14, 17, 20, 23]
fail-fast: false
steps:
- uses: actions/checkout@v7
- uses: actions/checkout@v6
- name: Add msbuild to PATH
uses: microsoft/setup-msbuild@v2
- name: cmake
@@ -160,7 +160,7 @@ jobs:
name: Build Windows 2022
runs-on: windows-2022
steps:
- uses: actions/checkout@v7
- uses: actions/checkout@v6
- name: Add msbuild to PATH
uses: microsoft/setup-msbuild@v2
- name: cmake
@@ -200,7 +200,7 @@ jobs:
'-p:EnableSpanT=true,UnsafeByteBuffer=true'
]
steps:
- uses: actions/checkout@v7
- uses: actions/checkout@v6
- name: Setup .NET Core SDK
uses: actions/setup-dotnet@v5
with:
@@ -229,7 +229,7 @@ jobs:
name: Build Mac (for Intel)
runs-on: macos-15-intel
steps:
- uses: actions/checkout@v7
- uses: actions/checkout@v6
- name: cmake
run: cmake -G "Xcode" -DCMAKE_OSX_ARCHITECTURES="x86_64" -DCMAKE_BUILD_TYPE=Release -DFLATBUFFERS_STRICT_MODE=ON .
- name: build
@@ -272,7 +272,7 @@ jobs:
name: Build Mac (universal build)
runs-on: macos-latest
steps:
- uses: actions/checkout@v7
- uses: actions/checkout@v6
- name: cmake
run: cmake -G "Xcode" -DCMAKE_OSX_ARCHITECTURES="arm64;x86_64" -DCMAKE_BUILD_TYPE=Release -DFLATBUFFERS_STRICT_MODE=ON .
- name: build
@@ -312,7 +312,7 @@ jobs:
if: false #disabled due to continual failure
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v7
- uses: actions/checkout@v6
- name: set up Java
uses: actions/setup-java@v5
with:
@@ -336,7 +336,7 @@ jobs:
matrix:
cxx: [g++-13, clang++-18]
steps:
- uses: actions/checkout@v7
- uses: actions/checkout@v6
- name: cmake
run: CXX=${{ matrix.cxx }} cmake -G "Unix Makefiles" -DFLATBUFFERS_BUILD_TESTS=OFF -DCMAKE_BUILD_TYPE=Release -DFLATBUFFERS_STRICT_MODE=ON . && make -j
- name: Generate
@@ -348,7 +348,7 @@ jobs:
name: Check Generated Code on Windows
runs-on: windows-2022
steps:
- uses: actions/checkout@v7
- uses: actions/checkout@v6
- name: Add msbuild to PATH
uses: microsoft/setup-msbuild@v2
- name: cmake
@@ -367,7 +367,7 @@ jobs:
matrix:
cxx: [g++-13]
steps:
- uses: actions/checkout@v7
- uses: actions/checkout@v6
- name: cmake
run: CXX=${{ matrix.cxx }} cmake -G "Unix Makefiles" -DFLATBUFFERS_CXX_FLAGS="-Wno-unused-parameter -fno-aligned-new" -DFLATBUFFERS_BUILD_BENCHMARKS=ON -DCMAKE_BUILD_TYPE=Release -DFLATBUFFERS_STRICT_MODE=ON . && make -j
- name: Run benchmarks
@@ -382,7 +382,7 @@ jobs:
name: Build Java
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v7
- uses: actions/checkout@v6
- name: test
working-directory: java
run: mvn test
@@ -392,7 +392,7 @@ jobs:
runs-on: macos-15
steps:
- name: Checkout
uses: actions/checkout@v7
uses: actions/checkout@v6
- name: set up Java
uses: actions/setup-java@v5
with:
@@ -415,7 +415,7 @@ jobs:
runs-on: ubuntu-24.04
steps:
- name: Checkout
uses: actions/checkout@v7
uses: actions/checkout@v6
- name: set up Java
uses: actions/setup-java@v5
with:
@@ -439,7 +439,7 @@ jobs:
name: Build Rust Linux
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v7
- uses: actions/checkout@v6
- name: test
working-directory: tests
run: bash RustTest.sh
@@ -448,7 +448,7 @@ jobs:
name: Build Rust Windows
runs-on: windows-2022
steps:
- uses: actions/checkout@v7
- uses: actions/checkout@v6
- name: test
working-directory: tests
run: ./RustTest.bat
@@ -457,7 +457,7 @@ jobs:
name: Build Python
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v7
- uses: actions/checkout@v6
- name: flatc
# FIXME: make test script not rely on flatc
run: cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Release -DFLATBUFFERS_BUILD_TESTS=OFF -DFLATBUFFERS_INSTALL=OFF -DFLATBUFFERS_BUILD_FLATLIB=OFF -DFLATBUFFERS_BUILD_FLATHASH=OFF -DFLATBUFFERS_STRICT_MODE=ON . && make -j
@@ -469,7 +469,7 @@ jobs:
name: Build Go
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v7
- uses: actions/checkout@v6
- name: flatc
# FIXME: make test script not rely on flatc
run: cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Release -DFLATBUFFERS_BUILD_TESTS=OFF -DFLATBUFFERS_INSTALL=OFF -DFLATBUFFERS_BUILD_FLATLIB=OFF -DFLATBUFFERS_BUILD_FLATHASH=OFF -DFLATBUFFERS_STRICT_MODE=ON . && make -j
@@ -481,7 +481,7 @@ jobs:
name: Build PHP
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v7
- uses: actions/checkout@v6
- name: flatc
# FIXME: make test script not rely on flatc
run: cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Release -DFLATBUFFERS_BUILD_TESTS=OFF -DFLATBUFFERS_INSTALL=OFF -DFLATBUFFERS_BUILD_FLATLIB=OFF -DFLATBUFFERS_BUILD_FLATHASH=OFF -DFLATBUFFERS_STRICT_MODE=ON . && make -j
@@ -498,7 +498,7 @@ jobs:
swift: ["6.0", "6.1", "6.2"]
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v7
- uses: actions/checkout@v6
- uses: swift-actions/setup-swift@v2
with:
swift-version: ${{ matrix.swift }}
@@ -511,7 +511,7 @@ jobs:
name: Test swift windows
runs-on: windows-latest
steps:
- uses: actions/checkout@v7
- uses: actions/checkout@v6
- uses: SwiftyLab/setup-swift@latest
with:
swift-version: '6.1'
@@ -522,7 +522,7 @@ jobs:
name: Test Swift Wasm
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v7
- uses: actions/checkout@v6
- uses: swift-actions/setup-swift@v2
with:
swift-version: 6.2.1
@@ -540,7 +540,7 @@ jobs:
name: Build TS
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v7
- uses: actions/checkout@v6
- name: flatc
# FIXME: make test script not rely on flatc
run: cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Release -DFLATBUFFERS_BUILD_TESTS=OFF -DFLATBUFFERS_INSTALL=OFF -DFLATBUFFERS_BUILD_FLATLIB=OFF -DFLATBUFFERS_BUILD_FLATHASH=OFF . && make -j
@@ -559,7 +559,7 @@ jobs:
name: Build Dart
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v7
- uses: actions/checkout@v6
- uses: dart-lang/setup-dart@v1
with:
sdk: stable
@@ -574,7 +574,7 @@ jobs:
name: Build Nim
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v7
- uses: actions/checkout@v6
- name: flatc
# FIXME: make test script not rely on flatc
run: cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Release -DFLATBUFFERS_BUILD_TESTS=OFF -DFLATBUFFERS_INSTALL=OFF -DFLATBUFFERS_BUILD_FLATLIB=OFF -DFLATBUFFERS_BUILD_FLATHASH=OFF . && make -j
@@ -590,7 +590,7 @@ jobs:
name: Bazel
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v7
- uses: actions/checkout@v6
# Explicitly use 8.5.1 until we can update or https://github.com/actions/runner-images/issues/13564 is fixed.
- name: Set env
run: >
+1 -1
View File
@@ -16,7 +16,7 @@ jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7
- uses: actions/checkout@v6
- name: Configure Git Credentials
run: |
git config user.name github-actions[bot]
+6 -6
View File
@@ -12,7 +12,7 @@ jobs:
name: Publish NPM
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7
- uses: actions/checkout@v6
- uses: actions/setup-node@v6
with:
node-version: '20.x'
@@ -29,7 +29,7 @@ jobs:
run:
working-directory: ./python
steps:
- uses: actions/checkout@v7
- uses: actions/checkout@v6
- uses: actions/setup-python@v6
with:
python-version: '3.10'
@@ -57,7 +57,7 @@ jobs:
run:
working-directory: ./net/flatbuffers
steps:
- uses: actions/checkout@v7
- uses: actions/checkout@v6
- uses: actions/setup-dotnet@v5
with:
dotnet-version: '8.0.x'
@@ -80,7 +80,7 @@ jobs:
run:
working-directory: ./java
steps:
- uses: actions/checkout@v7
- uses: actions/checkout@v6
- name: Set up Maven Central Repository
uses: actions/setup-java@v5
@@ -108,7 +108,7 @@ jobs:
run:
working-directory: ./kotlin
steps:
- uses: actions/checkout@v7
- uses: actions/checkout@v6
- name: Set up Maven Central Repository
uses: actions/setup-java@v5
with:
@@ -133,7 +133,7 @@ jobs:
name: Publish crates.io
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7
- uses: actions/checkout@v6
- uses: actions-rs/toolchain@v1
with:
toolchain: stable
+11
View File
@@ -75,6 +75,17 @@ extension Array where Element == Package.Dependency {
.package(
url: "https://github.com/grpc/grpc-swift-nio-transport.git",
from: "2.0.0"),
// swift-crypto arrives through grpc-swift-nio-transport -> swift-nio-ssl
// and is otherwise resolved afresh on every build, because this package
// ships no Package.resolved. 5.x asks for swift-tools-version 6.2, so only
// the 6.2 CI job selects it - and then fails to compile it, because 5.x
// uses `@nonexhaustive`, which the 6.2.x toolchains do not accept yet.
// Stay on the last 4.x release that still builds with every toolchain in
// the Swift CI matrix (4.3.0 declares swift-tools-version 6.0; 4.4+ asks
// for 6.1 and would drop the 6.0 job).
.package(
url: "https://github.com/apple/swift-crypto.git",
"4.0.0" ..< "4.4.0"),
]
#endif
}
-1
View File
@@ -504,7 +504,6 @@ grpc::string GenerateInterface(grpc_generator::File* file,
const grpc::string& filename) {
grpc::string output;
std::set<grpc::string> generated_functions;
std::map<grpc::string, grpc::string> vars;
vars["PATH"] = file->package();
+1 -1
View File
@@ -271,7 +271,7 @@ namespace flatbuffers {
#if (defined(_MSC_VER) && _MSC_VER >= 1800) || \
(defined(__ANDROID__) && defined(__ANDROID_API__) && __ANDROID_API__>= 26) || \
(defined(_XOPEN_VERSION) && (_XOPEN_VERSION >= 700)) && \
!defined(__Fuchsia__)
!defined(__Fuchsia__) && (!defined(__linux__) || defined(__GLIBC__))
#define FLATBUFFERS_LOCALE_INDEPENDENT 1
#else
#define FLATBUFFERS_LOCALE_INDEPENDENT 0
+8 -9
View File
@@ -24,15 +24,14 @@ class handle:
struct offset:
o:int
enum sizeof:
sz_8 = 1
sz_16 = 2
sz_32 = 4
sz_64 = 8
sz_voffset = 2
sz_uoffset = 4
sz_soffset = 4
sz_metadata_fields = 2
let sz_8 = 1
let sz_16 = 2
let sz_32 = 4
let sz_64 = 8
let sz_voffset = 2
let sz_uoffset = 4
let sz_soffset = 4
let sz_metadata_fields = 2
class builder:
buf = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
-2
View File
@@ -722,8 +722,6 @@ void BinaryAnnotator::BuildTable(const uint64_t table_offset,
BinaryRegionComment offset_field_comment;
offset_field_comment.type = BinaryRegionCommentType::TableOffsetField;
offset_field_comment.name = field->name()->c_str();
const std::string offset_prefix =
"offset to field `" + std::string(field->name()->c_str()) + "`";
// Validate any field that isn't inline (i.e., non-structs).
if (!IsInlineField(field)) {
+1 -3
View File
@@ -404,7 +404,7 @@ class CppGenerator : public BaseGenerator {
code_ += "struct {{STRUCT_NAME}}BinarySchema {";
code_ += " static const uint8_t *data() {";
code_ += " // Buffer containing the binary schema.";
code_ += " static const uint8_t bfbsData[" +
code_ += " alignas(uint64_t) static const uint8_t bfbsData[" +
NumToString(parser_.builder_.GetSize()) + "] = {";
code_ += binary_schema_hex_text;
code_ += " };";
@@ -2737,7 +2737,6 @@ class CppGenerator : public BaseGenerator {
if (!field.IsScalarOptional()) {
const bool is_scalar = IsScalar(type.base_type);
std::string accessor;
std::string offset_size = "";
if (is_scalar) {
accessor = "GetField<";
} else if (IsStruct(type)) {
@@ -2815,7 +2814,6 @@ class CppGenerator : public BaseGenerator {
void GenTableFieldType(const FieldDef& field) {
const auto& type = field.value.type;
const auto offset_str = GenFieldOffsetName(field);
if (!field.IsScalarOptional()) {
std::string afterptr = " *" + NullableExtension();
code_.SetValue("FIELD_TYPE",
-2
View File
@@ -1149,8 +1149,6 @@ static bool GenerateDart(const Parser& parser, const std::string& path,
static std::string DartMakeRule(const Parser& parser, const std::string& path,
const std::string& file_name) {
auto filebase =
flatbuffers::StripPath(flatbuffers::StripExtension(file_name));
dart::DartGenerator generator(parser, path, file_name);
auto make_rule = generator.Filename("") + ": ";
-2
View File
@@ -966,8 +966,6 @@ class KotlinGenerator : public BaseGenerator {
auto bbgetter = ByteBufferGetter(field.value.type, "bb");
auto ucast = CastToUsigned(field);
auto offset_val = NumToString(field.value.offset);
auto offset_prefix =
"val o = __offset(" + offset_val + "); return o != 0 ? ";
auto value_base_type = field.value.type.base_type;
// Most field accessors need to retrieve and test the field offset
// first, this is the offset value for that:
-5
View File
@@ -146,7 +146,6 @@ class KotlinKMPGenerator : public BaseGenerator {
std::string code =
"// " + std::string(FlatBuffersGeneratedWarning()) + "\n\n";
auto qualified = ns.GetFullyQualifiedName("");
std::string namespace_name = namer_.Namespace(ns);
if (!namespace_name.empty()) {
code += "package " + namespace_name;
@@ -341,7 +340,6 @@ class KotlinKMPGenerator : public BaseGenerator {
std::string GenDefaultBufferValue(const FieldDef& field) const {
auto& value = field.value;
auto base_type = value.type.base_type;
auto field_name = field.name;
std::string suffix = IsScalar(base_type) ? LiteralSuffix(value.type) : "";
if (field.IsScalarOptional()) {
return "null";
@@ -373,7 +371,6 @@ class KotlinKMPGenerator : public BaseGenerator {
std::string GenDefaultValue(const FieldDef& field) const {
auto& value = field.value;
auto base_type = value.type.base_type;
auto field_name = field.name;
std::string suffix = LiteralSuffix(value.type);
if (field.IsScalarOptional()) {
return "null";
@@ -1082,8 +1079,6 @@ class KotlinKMPGenerator : public BaseGenerator {
auto return_type = GetterReturnType(field);
auto bbgetter = ByteBufferGetter(field.value.type, "bb");
auto offset_val = NumToString(field.value.offset);
auto offset_prefix =
"val o = offset(" + offset_val + "); return o != 0 ? ";
auto value_base_type = field.value.type.base_type;
// Most field accessors need to retrieve and test the field offset
// first, this is the offset value for that:
-2
View File
@@ -2010,8 +2010,6 @@ class PythonGenerator : public BaseGenerator {
void InitializeFromPackedBuf(const StructDef& struct_def,
std::string* code_ptr) const {
auto& code = *code_ptr;
const auto struct_var = namer_.Variable(struct_def);
const auto struct_type = namer_.Type(struct_def);
code += GenIndents(1) + "@classmethod";
code += GenIndents(1) + "def InitFromPackedBuf(cls, buf, pos=0):";
-2
View File
@@ -787,7 +787,6 @@ class SwiftGenerator : public BaseGenerator {
}
void GenTableReaderFields(const FieldDef& field) {
const auto offset = NumToString(field.value.offset);
const auto field_field = namer_.Field(field);
const auto type = GenType(field.value.type);
code_.SetValue("FIELDVAR", namer_.Variable(field));
@@ -1382,7 +1381,6 @@ class SwiftGenerator : public BaseGenerator {
const auto& field = **it;
if (field.deprecated) continue;
const auto type = GenType(field.value.type);
code_.SetValue("FIELDVAR", namer_.Variable(field));
if (IsArray(field.value.type)) {
code_.SetValue(
-5
View File
@@ -196,7 +196,6 @@ class TsGenerator : public BaseGenerator {
SkipDir::OutputPathAndTrailingPathSeparator);
symbolic_name = definition.defined_namespace->components.back();
} else {
auto def_mod_name = namer_.File(definition, SkipFile::SuffixAndExtension);
symbolic_name = file_name_;
filepath = path_ + symbolic_name + ".ts";
}
@@ -286,12 +285,10 @@ class TsGenerator : public BaseGenerator {
auto base_name =
namer_.Directories(it.second.ns->components, SkipDir::OutputPath) +
base_file_name;
auto ts_file_path = base_name + ".ts";
auto base_name_rel = std::string("./");
base_name_rel +=
namer_.Directories(rel_components, SkipDir::OutputPath);
base_name_rel += base_file_name;
auto ts_file_path_rel = base_name_rel + ".ts";
auto type_name = def.first;
auto fully_qualified_type_name =
it.second.ns->GetFullyQualifiedName(type_name);
@@ -311,7 +308,6 @@ class TsGenerator : public BaseGenerator {
const auto child_ns_level = it.second.ns->components.size() + 1;
for (const auto& it2 : ns_defs_) {
if (it2.second.ns->components.size() != child_ns_level) continue;
auto ts_file_path = it2.second.path + ".ts";
code += "export * as " + it2.second.symbolic_name + " from './";
int count = it2.second.ns->components.size() > 1 ? 2 : 1;
std::vector<std::string> rel_path;
@@ -1176,7 +1172,6 @@ class TsGenerator : public BaseGenerator {
if (union_type.enum_def) {
const auto& enum_def = *union_type.enum_def;
const auto enum_type = AddImport(imports, dependent, enum_def).name;
const std::string union_accessor = "this." + field_name;
const auto union_has_string = UnionHasStringType(enum_def);
const auto field_binded_method = "this." + field_name + ".bind(this)";
+38 -8
View File
@@ -16,6 +16,30 @@
import Foundation
@inline(__always)
private func verifySizePrefix(
byteBuffer: inout ByteBuffer,
requireExactSize: Bool,
options: VerifierOptions) throws
{
let verifier = try Verifier(buffer: &byteBuffer, options: options)
let prefixPosition = byteBuffer.reader
let prefix: UOffset = try verifier.getValue(at: prefixPosition)
let availableSize = byteBuffer.size &- UOffset(MemoryLayout<UOffset>.size)
if requireExactSize {
guard prefix == availableSize else {
throw FlatbuffersErrors.prefixedSizeNotEqualToBufferSize
}
} else if prefix > availableSize {
throw FlatbuffersErrors.outOfBounds(
position: UInt(prefixPosition)
&+ UInt(MemoryLayout<UOffset>.size)
&+ UInt(prefix),
end: byteBuffer.capacity)
}
}
/// Takes in a prefixed sized buffer, where the prefixed size would be skipped.
/// And would verify that the buffer passed is a valid `Flatbuffers` Object.
/// - Parameters:
@@ -31,6 +55,10 @@ public func getPrefixedSizeCheckedRoot<T: FlatBufferTable & Verifiable>(
fileId: String? = nil,
options: VerifierOptions = .init()) throws -> T
{
try verifySizePrefix(
byteBuffer: &byteBuffer,
requireExactSize: false,
options: options)
byteBuffer.skipPrefix()
return try getCheckedRoot(
byteBuffer: &byteBuffer,
@@ -53,10 +81,11 @@ public func getCheckedPrefixedSizeRoot<T: FlatBufferTable & Verifiable>(
fileId: String? = nil,
options: VerifierOptions = .init()) throws -> T
{
let prefix = byteBuffer.skipPrefix()
if prefix != byteBuffer.size {
throw FlatbuffersErrors.prefixedSizeNotEqualToBufferSize
}
try verifySizePrefix(
byteBuffer: &byteBuffer,
requireExactSize: true,
options: options)
byteBuffer.skipPrefix()
return try getCheckedRoot(
byteBuffer: &byteBuffer,
fileId: fileId,
@@ -95,14 +124,15 @@ public func getCheckedRoot<T: FlatBufferTable & Verifiable>(
options: VerifierOptions = .init()) throws -> T
{
var verifier = try Verifier(buffer: &byteBuffer, options: options)
let rootPosition = byteBuffer.reader
if let fileId = fileId {
try verifier.verify(id: fileId)
try verifier.verify(id: fileId, at: rootPosition)
}
try ForwardOffset<T>.verify(&verifier, at: 0, of: T.self)
try ForwardOffset<T>.verify(&verifier, at: rootPosition, of: T.self)
return T.init(
byteBuffer,
o: Int32(byteBuffer.read(def: UOffset.self, position: byteBuffer.reader))
&+ Int32(byteBuffer.reader))
o: Int32(byteBuffer.read(def: UOffset.self, position: rootPosition))
&+ Int32(rootPosition))
}
/// Returns a `NON-Checked` flatbuffers object
+7 -3
View File
@@ -215,12 +215,16 @@ public struct Verifier {
}
@inline(__always)
func verify(id: String) throws {
func verify(id: String, at position: Int) throws {
let size = MemoryLayout<Int32>.size
guard storage.capacity >= (size &* 2) else {
guard
position >= 0,
position <= storage.capacity,
storage.capacity - position >= size &* 2
else {
throw FlatbuffersErrors.bufferDoesntContainID
}
let str = _buffer.readString(at: size, count: size)
let str = _buffer.readString(at: position &+ size, count: size)
if id == str {
return
}
+1 -1
View File
@@ -12,7 +12,7 @@ namespace Example {
struct MonsterBinarySchema {
static const uint8_t *data() {
// Buffer containing the binary schema.
static const uint8_t bfbsData[14520] = {
alignas(uint64_t) static const uint8_t bfbsData[14520] = {
0x20,0x00,0x00,0x00,0x42,0x46,0x42,0x53,0x00,0x00,0x00,0x00,0x14,0x00,0x20,0x00,0x04,0x00,0x08,0x00,
0x0C,0x00,0x10,0x00,0x14,0x00,0x18,0x00,0x00,0x00,0x1C,0x00,0x14,0x00,0x00,0x00,0x58,0x00,0x00,0x00,
0x34,0x00,0x00,0x00,0x24,0x00,0x00,0x00,0x18,0x00,0x00,0x00,0x70,0x0D,0x00,0x00,0x08,0x00,0x00,0x00,
@@ -373,6 +373,58 @@ final class FlatbuffersVerifierTests {
}
}
@Test
func testSizePrefixedVerifierUsesPostPrefixRoot() throws {
// The size prefix points to a valid empty decoy table at byte 44. The real
// root at byte 4 points to a truncated UInt64 vector that must be rejected.
let bytes: [UInt8] = [
44, 0, 0, 0,
16, 0, 0, 0,
6, 0, 8, 0, 4, 0,
0, 0, 0, 0, 0, 0,
12, 0, 0, 0,
8, 0, 0, 0, 0, 0, 0, 0,
2, 0, 0, 0, 65, 66,
0, 0,
4, 0, 4, 0,
4, 0, 0, 0,
]
var strictBuffer = ByteBuffer(bytes: bytes)
#expect(throws: FlatbuffersErrors.self) {
try getCheckedPrefixedSizeRoot(
byteBuffer: &strictBuffer) as Swift_Tests_Vectors
}
var prefixedBuffer = ByteBuffer(bytes: bytes)
#expect(throws: FlatbuffersErrors.self) {
try getPrefixedSizeCheckedRoot(
byteBuffer: &prefixedBuffer) as Swift_Tests_Vectors
}
var builder = FlatBufferBuilder()
let movie = Movie.createMovie(&builder)
Movie.finish(&builder, end: movie, prefix: true)
var validBuffer = builder.sizedBuffer
let _: Movie = try getCheckedPrefixedSizeRoot(
byteBuffer: &validBuffer,
fileId: Movie.id)
var shortBuffer = ByteBuffer(bytes: [0, 0, 0, 0])
#expect(throws: FlatbuffersErrors.bufferDoesntContainID) {
try getCheckedRoot(
byteBuffer: &shortBuffer,
fileId: Movie.id) as Movie
}
var oversizedPrefix = ByteBuffer(bytes: [5, 0, 0, 0, 0, 0, 0, 0])
#expect(throws: FlatbuffersErrors.outOfBounds(position: 9, end: 8)) {
try getPrefixedSizeCheckedRoot(
byteBuffer: &oversizedPrefix) as Movie
}
#expect(oversizedPrefix.reader == 0)
}
@Test
func testFullVerifier() throws {
_ =