Skip to content

Add bounds check for root offset in AddFlatBuffer#8982

Merged
jtdavis777 merged 2 commits intogoogle:masterfrom
rootvector2:addflatbuffer-bounds-check
Mar 19, 2026
Merged

Add bounds check for root offset in AddFlatBuffer#8982
jtdavis777 merged 2 commits intogoogle:masterfrom
rootvector2:addflatbuffer-bounds-check

Conversation

@rootvector2
Copy link
Contributor

AddFlatBuffer reads the root offset from the input buffer and uses it
for pointer arithmetic without validating that it lies within the buffer bounds.

This change adds a minimal bounds check before using the offset to avoid
undefined behavior when malformed data is passed.

No behavior change for valid inputs.

@github-actions github-actions bot added c++ codegen Involving generating code from schema labels Mar 17, 2026
Copy link
Collaborator

@jtdavis777 jtdavis777 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like a great change to have in place, my only comments are that flatbuffers is intended to be a very fast, low level library. There are many places in the code base that make the intentional trade off to favor speed over protection, and this seems like one of those places. This function is intending to be called from within someone's code where they have constructed a valid flatbuffers object to add in here -- what is the actual level of risk to leaving this function as is?

@rootvector2
Copy link
Contributor Author

I agree FlatBuffers often favors speed on hot paths. In this case, the added checks are intentionally minimal and are before work that already does buffer alignment and a full insert/copy, so the runtime impact should be negligible relative to existing cost.

The motivation is to avoid undefined behavior on malformed input in release builds (where assertions may be disabled): previously we could read/use an invalid root offset for pointer arithmetic. With this change, invalid input returns nullptr; valid input behavior is unchanged.

@jtdavis777 jtdavis777 merged commit 21b0332 into google:master Mar 19, 2026
51 checks passed
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.

2 participants