From e039a454cf4fb7cba5d4e1a3409487ceb133a44c Mon Sep 17 00:00:00 2001 From: Beman Date: Thu, 7 Sep 2017 16:36:48 -0400 Subject: [PATCH] Hand add the fix from github.com/boostorg/system/pull/12. Note: This fix was verified by testing in release mode with clang 4.0 on Linux. It crashed before applying the P/R and passed afterwards. The pull request was applied by hand to add some new comments and do some other minor code rearrangements. Thanks to Visigoth for the P/R. --- include/boost/system/error_code.hpp | 16 +++++++++++++--- test/error_code_test.cpp | 3 +++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/include/boost/system/error_code.hpp b/include/boost/system/error_code.hpp index 12191f9..080bb27 100644 --- a/include/boost/system/error_code.hpp +++ b/include/boost/system/error_code.hpp @@ -542,8 +542,9 @@ namespace boost // "throws" function in namespace boost rather than namespace boost::system. } // namespace system - - namespace detail { inline system::error_code * throws() { return 0; } } + + namespace detail + { // Misuse of the error_code object is turned into a noisy failure by // poisoning the reference. This particular implementation doesn't // produce warnings or errors from popular compilers, is very efficient @@ -551,8 +552,17 @@ namespace boost // from order of initialization problems. In practice, it also seems // cause user function error handling implementation errors to be detected // very early in the development cycle. + inline system::error_code* throws() + { + // See github.com/boostorg/system/pull/12 by visigoth for why the return + // is poisoned with (1) rather than (0). A test, test_throws_usage(), has + // been added to error_code_test.cpp, and as visigoth mentioned it fails + // on clang for release builds with a return of 0 but works fine with (1). + return reinterpret_cast(1); + } + } - inline system::error_code & throws() + inline system::error_code& throws() { return *detail::throws(); } namespace system diff --git a/test/error_code_test.cpp b/test/error_code_test.cpp index 043f23e..baf3853 100644 --- a/test/error_code_test.cpp +++ b/test/error_code_test.cpp @@ -99,6 +99,9 @@ namespace catch (...) { exception_thrown = true; } BOOST_TEST(exception_thrown); + + //error_code should_fail(boost::throws()); // should fail at runtime + //boost::throws() = ec; // should fail at runtime } }