aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMikeGitb <MikeGitb@users.noreply.github.com>2016-10-17 20:36:11 +0100
committerNeil MacIntosh <neilmac@microsoft.com>2016-10-17 12:36:11 -0700
commit1287e624cd3e23c7a3edc036de59690d733815d3 (patch)
tree1e6d78fa446d087ae001ac5348806e8565f2c395
parent9ef335ce32a2287a810fc51dd2f9abd63e852ddf (diff)
downloadplatform_external_Microsoft-GSL-1287e624cd3e23c7a3edc036de59690d733815d3.tar.gz
platform_external_Microsoft-GSL-1287e624cd3e23c7a3edc036de59690d733815d3.tar.bz2
platform_external_Microsoft-GSL-1287e624cd3e23c7a3edc036de59690d733815d3.zip
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
-rw-r--r--README.md4
-rw-r--r--tests/CMakeLists.txt4
-rw-r--r--tests/byte_tests.cpp16
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:<sup>1)</sup>
* 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.
+<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
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<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);
+ }
}
}