diff --git a/include/flatbuffers/flatbuffers.h b/include/flatbuffers/flatbuffers.h index 0ae297b39..0b9cf5853 100644 --- a/include/flatbuffers/flatbuffers.h +++ b/include/flatbuffers/flatbuffers.h @@ -416,7 +416,10 @@ class vector_downward { uint8_t *make_space(size_t len) { if (len > static_cast(cur_ - buf_)) { auto old_size = size(); + auto largest_align = AlignOf(); reserved_ += std::max(len, growth_policy(reserved_)); + // Round up to avoid undefined behavior from unaligned loads and stores. + reserved_ = (reserved_ + (largest_align - 1)) & ~(largest_align - 1); auto new_buf = allocator_.allocate(reserved_); auto new_cur = new_buf + reserved_ - old_size; memcpy(new_cur, cur_, old_size);