diff --git a/include/flatbuffers/flatbuffers.h b/include/flatbuffers/flatbuffers.h index 6966cb0af..855abc4df 100644 --- a/include/flatbuffers/flatbuffers.h +++ b/include/flatbuffers/flatbuffers.h @@ -892,10 +892,16 @@ inline voffset_t FieldIndexToOffset(voffset_t field_id) { template const T *data(const std::vector &v) { - return v.empty() ? nullptr : &v.front(); + // Eventually the returned pointer gets passed down to memcpy, so + // we need it to be non-null to avoid undefined behavior. + static uint8_t t; + return v.empty() ? reinterpret_cast(&t) : &v.front(); } template T *data(std::vector &v) { - return v.empty() ? nullptr : &v.front(); + // Eventually the returned pointer gets passed down to memcpy, so + // we need it to be non-null to avoid undefined behavior. + static uint8_t t; + return v.empty() ? reinterpret_cast(&t) : &v.front(); } /// @endcond