Skip to content

Hardening: add null guards, bounds checks, and optional safety to IDL generators and schema emitters - #9367

Closed
anagnorisis2peripeteia wants to merge 1 commit into
google:masterfrom
anagnorisis2peripeteia:fix-idl-generators-file-saver-and-fbs-schema-safety
Closed

anagnorisis2peripeteia wants to merge 1 commit into
google:masterfrom
anagnorisis2peripeteia:fix-idl-generators-file-saver-and-fbs-schema-safety

Conversation

@anagnorisis2peripeteia

Copy link
Copy Markdown

Description

This PR defensively hardens IDL code generators and schema emitters against null dereferences and iterator/container bounds issues:

  1. src/idl_gen_fbs.cpp:
    • Safeguards GenType against null struct_def or null defined_namespace.
    • Validates that used_proto_ids.size() >= 2 in HasTwiceUsedId and HasGapInProtoId before calling std::next(std::begin(...)) to eliminate undefined iterator advancing on small or empty vectors.
    • Guards against null FieldDef* entries in HasFieldWithId, HasNonPositiveFieldId, HasFieldIdFromReservedIds, ExtractProtobufIds, and MapProtoIdsToFieldsId.
    • In GenerateFBS: bounds-checks ns.from_table <= ns.components.size() before performing reverse component indexing and iterator subtractions; skips null namespace/enum/struct pointers; guards enum_def.defined_namespace and struct_def.defined_namespace; guards parser.opts.file_saver before invoking SaveFile.
  2. src/idl_gen_binary.cpp:
    • Guards parser.opts.file_saver in GenerateBinary for both flexbuffers and standard flatbuffers.
    • Guards against null parser.root_struct_def_ before accessing its file member in BinaryMakeRule.
  3. src/idl_gen_cpp.cpp:
    • Safeguards generate_bfbs_embed and generate against null parser_.opts.file_saver.
  4. src/idl_gen_text.cpp:
    • Checks parser.opts.file_saver in GenTextFile before calling SaveFile, returning a failure string rather than dereferencing null.
  5. tests/test.cpp:
    • Adds unit test IdlGeneratorsSafetyTest validating safe handling of null file_saver in GenTextFile and NewFBSCodeGenerator.

@github-actions github-actions Bot added c++ codegen Involving generating code from schema labels Oct 11, 2026
@anagnorisis2peripeteia

Copy link
Copy Markdown
Author

Closing this pull request.

These checks guard states a correct parse does not produce, or they treat a missing file_saver as a successful generate. FlatBuffers does not put that check in the generators. A schema problem is a parser error. A caller mistake is FLATBUFFERS_ASSERT. An untrusted buffer is checked in the verifier.

See the C++ guide section "Access of untrusted buffers" and the review on #8990.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

c++ codegen Involving generating code from schema

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant