Skip to content

Harden stl_emulation span assignment, make_span deduction, and bounds safety - #9349

Open
anagnorisis2peripeteia wants to merge 1 commit into
google:masterfrom
anagnorisis2peripeteia:fix-stl-emulation-span-and-optional-safety
Open

anagnorisis2peripeteia wants to merge 1 commit into
google:masterfrom
anagnorisis2peripeteia:fix-stl-emulation-span-and-optional-safety

Conversation

@anagnorisis2peripeteia

Copy link
Copy Markdown

Description

This change hardens the STL emulation utility header (include/flatbuffers/stl_emulation.h):

  1. Span assignment & rebinding: In flatbuffers::span, data_ was declared pointer const data_, making copy assignment ill-formed when instantiated. Furthermore, operator= was missing a return statement (return *this;). Fixed data_ to be non-const so the span view is rebindable (matching C++20 std::span) and added return *this;.
  2. make_span deduction: Removed the non-deducible Extent template parameter from the pointer overloads of make_span(first, count), allowing caller template argument deduction without requiring manual instantiation of <ElementType, Extent>.
  3. Span bounds safety: Added FLATBUFFERS_ASSERT(idx < size()) in span::operator[] to catch out-of-bounds accesses.
  4. Optional safety: Added FLATBUFFERS_ASSERT(has_value()) in Optional::operator*() to guard against dereferencing an empty optional.

Testing

  • Added StlEmulationSafetyTest in tests/test.cpp verifying span copy-assignment rebinding, make_span template deduction on mutable and const pointers, and optional value access.
  • All unit tests in flattests pass.

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant