forked from google/flatbuffers
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[C++] Rare bad buffer content alignment if sizeof(T) != alignof(T) (g…
…oogle#7520) * [C++] Add a failing unit test for google#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 google#7516
- Loading branch information
Showing
10 changed files
with
611 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// sizeof(BadAlignmentSmall) == 12 | ||
// alignof(BadAlignmentSmall) == 4 | ||
struct BadAlignmentSmall { | ||
var_0: uint; | ||
var_1: uint; | ||
var_2: uint; | ||
} | ||
|
||
// sizeof(BadAlignmentLarge) == 8 | ||
// alignof(BadAlignmentLarge) == 8 | ||
struct BadAlignmentLarge { | ||
var_0: ulong; | ||
} | ||
|
||
table OuterLarge { | ||
large: BadAlignmentLarge; | ||
} | ||
|
||
table BadAlignmentRoot { | ||
large: OuterLarge; | ||
small: [BadAlignmentSmall]; | ||
} | ||
|
||
root_type BadAlignmentRoot; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#ifndef TESTS_ALIGNMENT_TEST_H | ||
#define TESTS_ALIGNMENT_TEST_H | ||
|
||
namespace flatbuffers { | ||
namespace tests { | ||
|
||
void AlignmentTest(); | ||
|
||
} // namespace tests | ||
} // namespace flatbuffers | ||
|
||
#endif |
Oops, something went wrong.