mirror of
https://github.com/microsoft/GSL.git
synced 2024-11-03 17:56:43 -05:00
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:
parent
9ef335ce32
commit
1287e624cd
@ -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:
|
||||
|
||||
|
@ -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()
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user