Fixed potential strict-aliasing violation in big-endian code.

Also added a test.

Tested on: Linux.

Change-Id: I7b3230f8f6043eec139d5e3e8c9cb45814124274
This commit is contained in:
Wouter van Oortmerssen
2017-11-16 14:19:31 -08:00
parent 7e803c410c
commit 8a8dc4e111
2 changed files with 23 additions and 6 deletions

View File

@@ -1803,6 +1803,16 @@ void TypeAliasesTest()
TEST_EQ(sizeof(ta->f64()), 8);
}
void EndianSwapTest() {
TEST_EQ(flatbuffers::EndianSwap(static_cast<int16_t>(0x1234)),
0x3412);
TEST_EQ(flatbuffers::EndianSwap(static_cast<int32_t>(0x12345678)),
0x78563412);
TEST_EQ(flatbuffers::EndianSwap(static_cast<int64_t>(0x1234567890ABCDEF)),
0xEFCDAB9078563412);
TEST_EQ(flatbuffers::EndianSwap(flatbuffers::EndianSwap(3.14f)), 3.14f);
}
int main(int /*argc*/, const char * /*argv*/[]) {
#if defined(FLATBUFFERS_MEMORY_LEAK_TRACKING) && \
defined(_MSC_VER) && defined(_DEBUG)
@@ -1866,6 +1876,7 @@ int main(int /*argc*/, const char * /*argv*/[]) {
ConformTest();
ParseProtoBufAsciiTest();
TypeAliasesTest();
EndianSwapTest();
FlexBuffersTest();