flatc --annotate: don't crash on a .bfbs with no root_table#9104
Open
inigofox wants to merge 1 commit into
Open
flatc --annotate: don't crash on a .bfbs with no root_table#9104inigofox wants to merge 1 commit into
flatc --annotate: don't crash on a .bfbs with no root_table#9104inigofox wants to merge 1 commit into
Conversation
Signed-off-by: Ian Brandeberry <ianbrandeberry@gmail.com>
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
flatc --annotate: don't crash on a.bfbswith noroot_tableSummary
BinaryAnnotator::Annotate()unconditionally dereferencesRootTable()in two places (
src/binary_annotator.cpp:149and:210), butreflection::Schema.root_tableis an optional field perreflection/reflection.fbsand theschema verifier does not enforce its presence. A valid
.bfbscompiled from a
.fbswith noroot_typedirective therefore makesflatc --annotate <schema.bfbs> <binary>crash with aSIGSEGV(nullread inside
BuildHeader).This PR adds a single defensive early-return at the top of
Annotate()— mirroring the existing fix forbfbs_gen_lua.cppin#8770 — that bails gracefully (returns an empty sections map) when the
schema does not declare a root table or when the
--root-type-style override names a table that is not present inschema_->objects().Reproducer (before this PR, on
master @ 1f438bd4)cmake -B build -DCMAKE_BUILD_TYPE=Debug \ -DCMAKE_CXX_FLAGS='-fsanitize=address,undefined' cmake --build build --target flatc -j echo 'namespace x; table T { v:int; }' > empty_root.fbs ./build/flatc -b --schema empty_root.fbs # produces empty_root.bfbs printf '\x04\x00\x00\x00ABCDEFGH' > bin.dat ./build/flatc --annotate empty_root.bfbs bin.datASAN output (pre-patch):
After this PR:
flatc --annotateexits cleanly with no output (noannotations possible) and exit code 0.
Why the verifier does not already catch this
reflection/reflection.fbsdeclaresroot_table:Object;— without a(required)attribute — soreflection::VerifySchemaBuffer()rightly accepts schemas that omitit. The bug lives in the annotator's implicit assumption that the
field is always present.
Why a single
Annotate()-level guard is enoughAnnotate()is the only public entry point onBinaryAnnotator.Returning early there protects every downstream dereference of
RootTable()in this code path:binary_annotator.cpp:149BuildTable(..., RootTable())binary_annotator.cpp:210RootTable()->name()->str()Related work
ac8b1244) — applied the equivalent guard tobfbs_gen_lua.cppfor the same root-table-may-be-null hazard in theLua bfbs generator. This PR closes the parity gap in the binary
annotator.
Test impact
make test) continue to pass.tests/fuzzer/flatbuffers_annotator_fuzzer.cchard-codes a schemawith a
root_tableand is unaffected. (It also did not exercisethis code path before this PR; a follow-up harness that also fuzzes
the schema would be valuable but is out of scope here.)
Files
src/binary_annotator.cpp—+10 / -0Compatibility / behavior change
The only behavior change is:
flatc --annotateon a.bfbswith noroot_tablenow exits cleanly with no annotation output instead ofcrashing. No public API change.