From 61534ca3adda0e214d6fb7dd9bc4173edb6e02d1 Mon Sep 17 00:00:00 2001 From: Nicholas Londey Date: Wed, 22 Apr 2020 10:09:13 +1000 Subject: [PATCH 1/5] Changed implementation of gsl::narrow to throw gsl::narrowing_error Implementation now behaves as described in the C++ Core Guidlines --- include/gsl/gsl_util | 8 ++++++-- tests/utils_tests.cpp | 12 +++--------- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/include/gsl/gsl_util b/include/gsl/gsl_util index d1f7f33..bc65923 100644 --- a/include/gsl/gsl_util +++ b/include/gsl/gsl_util @@ -96,6 +96,10 @@ constexpr T narrow_cast(U&& u) noexcept return static_cast(std::forward(u)); } +struct narrowing_error : public std::exception +{ +}; + namespace details { template @@ -115,9 +119,9 @@ constexpr T narrow(U u) noexcept(false) { T t = narrow_cast(u); - if (static_cast(t) != u) gsl::details::terminate(); + if (static_cast(t) != u) throw narrowing_error{}; if (!details::is_same_signedness::value && ((t < T{}) != (u < U{}))) - gsl::details::terminate(); + throw narrowing_error{}; return t; } diff --git a/tests/utils_tests.cpp b/tests/utils_tests.cpp index f7f7ce3..98faf58 100644 --- a/tests/utils_tests.cpp +++ b/tests/utils_tests.cpp @@ -104,11 +104,6 @@ TEST(utils_tests, narrow_cast) TEST(utils_tests, narrow) { - std::set_terminate([] { - std::cerr << "Expected Death. narrow"; - std::abort(); - }); - int n = 120; const char c = narrow(n); EXPECT_TRUE(c == 120); @@ -123,14 +118,13 @@ TEST(utils_tests, narrow) EXPECT_TRUE(narrow(int32_t(1)) == 1); EXPECT_TRUE(narrow(int32_max) == static_cast(int32_max)); - EXPECT_DEATH(narrow(int32_t(-1)), deathstring); - EXPECT_DEATH(narrow(int32_min), deathstring); + EXPECT_THROW(narrow(int32_t(-1)), narrowing_error); + EXPECT_THROW(narrow(int32_min), narrowing_error); n = -42; - EXPECT_DEATH(narrow(n), deathstring); + EXPECT_THROW(narrow(n), narrowing_error); #if GSL_CONSTEXPR_NARROW static_assert(narrow(120) == 120, "Fix GSL_CONSTEXPR_NARROW"); #endif - } From 4eb554d7c27e8ac8955c26410f44566ae3e661d6 Mon Sep 17 00:00:00 2001 From: Nicholas Londey Date: Thu, 23 Apr 2020 11:58:00 +1000 Subject: [PATCH 2/5] Removed no_exception_throw_tests.cpp as per review feedback --- tests/CMakeLists.txt | 1 - tests/no_exception_throw_tests.cpp | 48 ------------------------------ 2 files changed, 49 deletions(-) delete mode 100644 tests/no_exception_throw_tests.cpp diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 0219319..2bd1bbe 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -264,5 +264,4 @@ function(add_gsl_test_noexcept name) set_property(TARGET ${name} PROPERTY FOLDER "GSL_tests_noexcept") endfunction() -add_gsl_test_noexcept(no_exception_throw_tests) add_gsl_test_noexcept(no_exception_ensure_tests) diff --git a/tests/no_exception_throw_tests.cpp b/tests/no_exception_throw_tests.cpp deleted file mode 100644 index ddb6b07..0000000 --- a/tests/no_exception_throw_tests.cpp +++ /dev/null @@ -1,48 +0,0 @@ -/////////////////////////////////////////////////////////////////////////////// -// -// Copyright (c) 2015 Microsoft Corporation. All rights reserved. -// -// This code is licensed under the MIT License (MIT). -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -// THE SOFTWARE. -// -/////////////////////////////////////////////////////////////////////////////// - -#include // for std::exit -#include // for get_terminate -#include // for narrow - -int narrow_no_throw() -{ - const long long bigNumber = 0x0fffffffffffffff; - return gsl::narrow(bigNumber); -} - -[[noreturn]] void test_terminate() { std::exit(0); } - -void setup_termination_handler() noexcept -{ -#if defined(GSL_MSVC_USE_STL_NOEXCEPTION_WORKAROUND) - - auto& handler = gsl::details::get_terminate_handler(); - handler = &test_terminate; - -#else - - std::set_terminate(test_terminate); - -#endif -} - -int main() -{ - setup_termination_handler(); - narrow_no_throw(); - return -1; -} From 14acdcd7a8bd4b9e3df4eaeafb398ec11d219b5a Mon Sep 17 00:00:00 2001 From: Nicholas Londey Date: Thu, 23 Apr 2020 12:22:30 +1000 Subject: [PATCH 3/5] Removed unused #include --- include/gsl/span | 1 - 1 file changed, 1 deletion(-) diff --git a/include/gsl/span b/include/gsl/span index c011d76..b4e791f 100644 --- a/include/gsl/span +++ b/include/gsl/span @@ -19,7 +19,6 @@ #include // for Expects #include // for byte -#include // for narrow_cast, narrow #include // for array #include // for ptrdiff_t, size_t, nullptr_t From 94d6a35726d676b677ce4559ddb3e76bae14f98e Mon Sep 17 00:00:00 2001 From: Nicholas Londey Date: Thu, 23 Apr 2020 12:23:36 +1000 Subject: [PATCH 4/5] Fixed narrow unit test EXPECT_THROW --- tests/utils_tests.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/utils_tests.cpp b/tests/utils_tests.cpp index 98faf58..a9408c0 100644 --- a/tests/utils_tests.cpp +++ b/tests/utils_tests.cpp @@ -109,7 +109,7 @@ TEST(utils_tests, narrow) EXPECT_TRUE(c == 120); n = 300; - EXPECT_DEATH(narrow(n), deathstring); + EXPECT_THROW(narrow(n), narrowing_error); const auto int32_max = std::numeric_limits::max(); const auto int32_min = std::numeric_limits::min(); From 3a5b83db35b95729b1fe81604f32a5d4b0bf274e Mon Sep 17 00:00:00 2001 From: Nicholas Londey Date: Thu, 23 Apr 2020 12:30:44 +1000 Subject: [PATCH 5/5] Removed unused contant from test --- tests/utils_tests.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/utils_tests.cpp b/tests/utils_tests.cpp index a9408c0..f6d9fd7 100644 --- a/tests/utils_tests.cpp +++ b/tests/utils_tests.cpp @@ -29,7 +29,6 @@ using namespace gsl; namespace { -static constexpr char deathstring[] = "Expected Death"; void f(int& i) { i += 1; } static int j = 0; void g() { j += 1; }