Skip to content

Commit

Permalink
[C++] insert null into vector (google#8424)
Browse files Browse the repository at this point in the history
  • Loading branch information
boyism80 committed Oct 10, 2024
1 parent 2f59a03 commit 2659ad1
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
2 changes: 2 additions & 0 deletions include/flatbuffers/buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ struct IndirectHelper<OffsetT<T>> {
// Offsets are relative to themselves, so first update the pointer to
// point to the offset location.
const uint8_t *const offset_location = p + i * element_stride;
if (*offset_location == 0)
return nullptr;

// Then read the scalar value of the offset (which may be 32 or 64-bits) and
// then determine the relative location from the offset location.
Expand Down
9 changes: 6 additions & 3 deletions include/flatbuffers/flatbuffer_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,10 @@ template<bool Is64Aware = false> class FlatBufferBuilderImpl {
template<typename T, template<typename> class OffsetT = Offset>
uoffset_t PushElement(OffsetT<T> off) {
// Special case for offsets: see ReferTo below.
return PushElement(ReferTo(off.o));
if (off.o == 0)
return PushElement(0);
else
return PushElement(ReferTo(off.o));;
}

// When writing fields, we track where they are, so we can create correct
Expand Down Expand Up @@ -381,12 +384,12 @@ template<bool Is64Aware = false> class FlatBufferBuilderImpl {
}

template<typename T, typename T2> T ReferTo(const T off, const T2 size) {
FLATBUFFERS_ASSERT(off && off <= size);
FLATBUFFERS_ASSERT(off <= size);
return size - off + static_cast<T>(sizeof(T));
}

template<typename T> T ReferTo(const T off, const T size) {
FLATBUFFERS_ASSERT(off && off <= size);
FLATBUFFERS_ASSERT(off <= size);
return size - off + static_cast<T>(sizeof(T));
}

Expand Down

0 comments on commit 2659ad1

Please sign in to comment.