From 1287e624cd3e23c7a3edc036de59690d733815d3 Mon Sep 17 00:00:00 2001 From: MikeGitb Date: Mon, 17 Oct 2016 20:36:11 +0100 Subject: 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 --- README.md | 4 +++- tests/CMakeLists.txt | 4 ++-- tests/byte_tests.cpp | 16 +++++++++++++++- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 9673df5..c3aff66 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ This project has adopted the [Microsoft Open Source Code of Conduct](https://ope # Quick Start ## 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:1) * Windows using Visual Studio 2013 * 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 contributing any changes that were necessary back to this project to benefit the wider community. +1) For `gsl::byte` to work correctly with Clang and GCC you might have to use the ` -fno-strict-aliasing` compiler option. + ## Building the tests To build the tests, you will require the following: diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 3c3125e..e480474 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -24,9 +24,9 @@ else() CHECK_CXX_COMPILER_FLAG("-std=c++14" COMPILER_SUPPORTS_CXX14) CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11) 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) - 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() message(STATUS "The compiler ${CMAKE_CXX_COMPILER} has no C++11 support. Please use a different C++ compiler.") endif() diff --git a/tests/byte_tests.cpp b/tests/byte_tests.cpp index 3bbf382..8cb0da8 100644 --- a/tests/byte_tests.cpp +++ b/tests/byte_tests.cpp @@ -43,7 +43,7 @@ SUITE(byte_tests) byte b = byte(12); CHECK(static_cast(b) == 12); } - + { byte b = to_byte<12>(); CHECK(static_cast(b) == 12); @@ -114,6 +114,20 @@ SUITE(byte_tests) // CHECK(0x12 == gsl::to_integer(b)); // expect compile-time error // CHECK(0x12 == gsl::to_integer(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(i), i); + CHECK(res == i); + } } } -- cgit v1.2.3