Address #313: try to guard against strict-aliasing bugs with gsl::byte

* Add test to demonstrate byte aliasing problem on g++ and clang++

* Add note about no-strict-aliasing flag in README

* Activate aliasing unit test and use -fno-strict-aliasing flag
This commit is contained in:
MikeGitb
2016-10-17 12:36:11 -07:00
committed by Neil MacIntosh
parent 9ef335ce32
commit 1287e624cd
3 changed files with 20 additions and 4 deletions
+15 -1
View File
@@ -43,7 +43,7 @@ SUITE(byte_tests)
byte b = byte(12);
CHECK(static_cast<unsigned char>(b) == 12);
}
{
byte b = to_byte<12>();
CHECK(static_cast<unsigned char>(b) == 12);
@@ -114,6 +114,20 @@ SUITE(byte_tests)
// CHECK(0x12 == gsl::to_integer<float>(b)); // expect compile-time error
// CHECK(0x12 == gsl::to_integer<double>(b)); // expect compile-time error
}
int modify_both(gsl::byte& b, int& i)
{
i = 10;
b = to_byte<5>();
return i;
}
TEST(aliasing)
{
int i{ 0 };
int res = modify_both(reinterpret_cast<byte&>(i), i);
CHECK(res == i);
}
}
}