Skip to content

Hardening: add null guards, bounds checks, and file saver safety to language IDL generators - #9371

Open
anagnorisis2peripeteia wants to merge 1 commit into
google:masterfrom
anagnorisis2peripeteia:fix-idl-codegen-languages-and-file-saver-safety
Open

anagnorisis2peripeteia wants to merge 1 commit into
google:masterfrom
anagnorisis2peripeteia:fix-idl-codegen-languages-and-file-saver-safety

Conversation

@anagnorisis2peripeteia

Copy link
Copy Markdown

Description

This PR strengthens defensive engineering across FlatBuffers language IDL generators (Swift, TypeScript, Rust, Python, Java, Kotlin, C#, Dart, PHP, Lobster, Text, and gRPC):

  1. src/idl_gen_swift.cpp:

    • Check parser_.opts.file_saver against nullptr before invoking SaveFile.
    • Add null guards in parser_.enums_.vec and parser_.structs_.vec loops before dereferencing **it.
  2. src/idl_gen_ts.cpp:

    • Check definition.defined_namespace against nullptr in SaveType.
    • Guard parser_.opts.file_saver against nullptr.
    • In TrackNsDef child namespace re-export loops, guard it.second.ns and it2.second.ns against null and verify !it2.second.ns->components.empty() before iterator subtraction (end() - count).
  3. src/idl_gen_rust.cpp:

    • Check parser.opts.file_saver against nullptr in GenerateRustModuleRootFile.
    • Guard symbol.defined_namespace and parser_.opts.file_saver against null in GenerateIndividualFiles.
    • Guard parser_.opts.file_saver against null in GenerateSingleFile.
  4. src/idl_gen_python.cpp:

    • Guard parser_.opts.file_saver in PythonStubGenerator::SaveFile and PythonGenerator::SaveType.
    • In PythonGenerator::SaveType, verify path_.size() <= directories.size() before calling std::string::find to prevent out-of-bounds indexing.
    • Null-check parser_.opts.file_saver when creating empty __init__.py files.
  5. src/idl_gen_java.cpp:

    • Guard parser_.opts.file_saver against nullptr in JavaGenerator::SaveType.
  6. src/idl_gen_kotlin.cpp & src/idl_gen_kotlin_kmp.cpp:

    • Guard parser_.opts.file_saver against nullptr in KotlinGenerator::SaveType and KotlinKmpGenerator::SaveType.
  7. src/idl_gen_csharp.cpp:

    • Guard options.file_saver against nullptr in CSharpGenerator::SaveType.
  8. src/idl_gen_dart.cpp:

    • Guard parser_.opts.file_saver against nullptr before invoking SaveFile.
  9. src/idl_gen_php.cpp:

    • Guard def.defined_namespace and parser_.opts.file_saver against null in PhpGenerator::SaveType.
  10. src/idl_gen_lobster.cpp:

    • Guard *it in structs loop against null.
    • Guard parser_.opts.file_saver against null in LobsterGenerator::generate.
  11. src/idl_gen_text.cpp:

    • Guard parser.opts.file_saver against null in GenTextFile flexbuffers and normal text paths.
  12. src/idl_gen_grpc.cpp:

    • Guard def and def->defined_namespace in GoGRPCGenerator and JavaGRPCGenerator.
    • Guard *it against null in parser.services_.vec iteration across GenerateGoGRPC, GenerateCppGRPC, GenerateJavaGRPC, GeneratePythonGRPC, GenerateSwiftGRPC.
    • Guard parser.opts.file_saver against null across C++, Java, Swift, and TypeScript gRPC generators.
  13. tests/test.cpp:

    • Added unit test IdlLanguageGeneratorsSafetyTest() verifying that GenerateTextFile fails cleanly and returns SaveFile failed when opts.file_saver == nullptr.

All tests compile cleanly under -DFLATBUFFERS_STRICT_MODE=ON and pass with flattests and flatc_cpp_tests.py.

This branch has not been deployed

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant