diff --git a/include/flatbuffers/buffer.h b/include/flatbuffers/buffer.h index 94d4f7903be..a343e67bfbc 100644 --- a/include/flatbuffers/buffer.h +++ b/include/flatbuffers/buffer.h @@ -116,6 +116,8 @@ struct IndirectHelper> { // 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. diff --git a/include/flatbuffers/flatbuffer_builder.h b/include/flatbuffers/flatbuffer_builder.h index 9ceca8207b6..128b1eb00ea 100644 --- a/include/flatbuffers/flatbuffer_builder.h +++ b/include/flatbuffers/flatbuffer_builder.h @@ -317,7 +317,10 @@ template class FlatBufferBuilderImpl { template class OffsetT = Offset> uoffset_t PushElement(OffsetT 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 @@ -381,12 +384,12 @@ template class FlatBufferBuilderImpl { } template T ReferTo(const T off, const T2 size) { - FLATBUFFERS_ASSERT(off && off <= size); + FLATBUFFERS_ASSERT(off <= size); return size - off + static_cast(sizeof(T)); } template T ReferTo(const T off, const T size) { - FLATBUFFERS_ASSERT(off && off <= size); + FLATBUFFERS_ASSERT(off <= size); return size - off + static_cast(sizeof(T)); }