Repository navigation
Hardening: add null guards, bounds checks, and return correctness to allocators, vector iterators, and stl emulation - #9369
Open
anagnorisis2peripeteia wants to merge 1 commit into
Conversation
…allocators, vector iterators, and stl emulation
This branch has not been deployed
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.
Description
This PR strengthens defensive engineering across FlatBuffers memory allocation, vector iterators, and STL emulation utilities:
include/flatbuffers/allocator.h:allocate(new_size)returnsnullptrbefore performing downward copies and deallocating inreallocate_downward.memcpy_downward, verify pointers (new_p,old_p) are non-null and copy counts (in_use_back,in_use_front) are non-zero before invokingmemcpyto avoid undefined behavior.include/flatbuffers/default_allocator.h:allocate(size)(size ? new uint8_t[size] : nullptr).delete[]indeallocate,dealloc, and helperDeallocate.include/flatbuffers/stl_emulation.h:return *this;inspan::operator=(const span &other)which previously fell off the end of a value-returning function (undefined behavior).pointer const data_;inspantopointer data_;so copy assignment can actually rebind the data pointer as specified.operator*,operator->, and mutablevalue()overloads toOptionalmatching standardstd::optionalsemantics.include/flatbuffers/vector.h:VectorIterator, avoid null pointer arithmetic (nullptr + offset) during construction and iterator offset operations.VectorIterator::operator-, check forelement_stride != 0before division to avoid division by zero.Vector::LookupByKey, returnnullptrimmediately ifsize() == 0rather than executing binary search on empty ranges.Vector::KeyCompare, add null guards forkey,data, andtablebefore dereference.make_structs_spanpointers overloads wheremake_spanwas invoked instead ofmake_structs_span.VectorCast, check fornullptrbefore reinterpret casting.tests/test.cpp:StlEmulationAndVectorSafetyTest()validatingOptional,spanassignment,VectorIterator,VectorCast, and null-safe allocator behaviors.All tests compile cleanly under
-DFLATBUFFERS_STRICT_MODE=ONand pass withflattestsandflatc_cpp_tests.py.