mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-02 12:05:50 +00:00
[C++] Rare bad buffer content alignment if sizeof(T) != alignof(T) (#7520)
* [C++] Add a failing unit test for #7516 (Rare bad buffer content alignment if sizeof(T) != alignof(T)) * [C++] Fix final buffer alignment when using an array of structs * A struct can have an arbitrary size and therefore sizeof(struct) == alignof(struct) does not hold anymore as for value primitives. * This patch fixes this by introducing alignment parameters to various CreateVector*/StartVector calls. * Closes #7516
This commit is contained in:
31
tests/alignment_test.cpp
Normal file
31
tests/alignment_test.cpp
Normal file
@@ -0,0 +1,31 @@
|
||||
#include "alignment_test.h"
|
||||
|
||||
#include "flatbuffers/flatbuffer_builder.h"
|
||||
#include "alignment_test_generated.h"
|
||||
#include "test_assert.h"
|
||||
|
||||
namespace flatbuffers {
|
||||
namespace tests {
|
||||
|
||||
void AlignmentTest() {
|
||||
FlatBufferBuilder builder;
|
||||
|
||||
BadAlignmentLarge large;
|
||||
Offset<OuterLarge> outer_large = CreateOuterLarge(builder, &large);
|
||||
|
||||
BadAlignmentSmall *small;
|
||||
Offset<Vector<const BadAlignmentSmall *>> small_offset =
|
||||
builder.CreateUninitializedVectorOfStructs(9, &small);
|
||||
(void)small; // We do not have to write data to trigger the test failure
|
||||
|
||||
Offset<BadAlignmentRoot> root =
|
||||
CreateBadAlignmentRoot(builder, outer_large, small_offset);
|
||||
|
||||
builder.Finish(root);
|
||||
|
||||
Verifier verifier(builder.GetBufferPointer(), builder.GetSize());
|
||||
TEST_ASSERT(VerifyBadAlignmentRootBuffer(verifier));
|
||||
}
|
||||
|
||||
} // namespace tests
|
||||
} // namespace flatbuffers
|
||||
Reference in New Issue
Block a user