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 20:36:11 +01:00 committed by Neil MacIntosh
parent 9ef335ce32
commit 1287e624cd
3 changed files with 20 additions and 4 deletions

View File

@ -19,7 +19,7 @@ This project has adopted the [Microsoft Open Source Code of Conduct](https://ope
# Quick Start # Quick Start
## Supported Platforms ## Supported Platforms
The test suite that exercises GSL has been built and passes successfully on the following platforms: The test suite that exercises GSL has been built and passes successfully on the following platforms:<sup>1)</sup>
* Windows using Visual Studio 2013 * Windows using Visual Studio 2013
* Windows using Visual Studio 2015 * Windows using Visual Studio 2015
@ -34,6 +34,8 @@ The test suite that exercises GSL has been built and passes successfully on the
> If you successfully port GSL to another platform, we would love to hear from you. Please submit an issue to let us know. Also please consider > If you successfully port GSL to another platform, we would love to hear from you. Please submit an issue to let us know. Also please consider
contributing any changes that were necessary back to this project to benefit the wider community. contributing any changes that were necessary back to this project to benefit the wider community.
<sup>1)</sup> For `gsl::byte` to work correctly with Clang and GCC you might have to use the ` -fno-strict-aliasing` compiler option.
## Building the tests ## Building the tests
To build the tests, you will require the following: To build the tests, you will require the following:

View File

@ -24,9 +24,9 @@ else()
CHECK_CXX_COMPILER_FLAG("-std=c++14" COMPILER_SUPPORTS_CXX14) CHECK_CXX_COMPILER_FLAG("-std=c++14" COMPILER_SUPPORTS_CXX14)
CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11) CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11)
if(COMPILER_SUPPORTS_CXX14) if(COMPILER_SUPPORTS_CXX14)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -Wall -Wno-missing-braces") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-strict-aliasing -std=c++14 -O3 -Wall -Wno-missing-braces")
elseif(COMPILER_SUPPORTS_CXX11) elseif(COMPILER_SUPPORTS_CXX11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -Wno-missing-braces") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-strict-aliasing -std=c++11 -O3 -Wall -Wno-missing-braces")
else() else()
message(STATUS "The compiler ${CMAKE_CXX_COMPILER} has no C++11 support. Please use a different C++ compiler.") message(STATUS "The compiler ${CMAKE_CXX_COMPILER} has no C++11 support. Please use a different C++ compiler.")
endif() endif()

View File

@ -43,7 +43,7 @@ SUITE(byte_tests)
byte b = byte(12); byte b = byte(12);
CHECK(static_cast<unsigned char>(b) == 12); CHECK(static_cast<unsigned char>(b) == 12);
} }
{ {
byte b = to_byte<12>(); byte b = to_byte<12>();
CHECK(static_cast<unsigned char>(b) == 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<float>(b)); // expect compile-time error
// CHECK(0x12 == gsl::to_integer<double>(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);
}
} }
} }