From 2aff45146f1464ba8899368ad004522cb6a1a98c Mon Sep 17 00:00:00 2001 From: Jacques Lucke Date: Wed, 19 Aug 2020 16:44:53 +0200 Subject: BLI: improve exception safety of Vector, Array and Stack Using C++ exceptions in Blender is difficult, due to the large number of C functions in the call stack. However, C++ data structures in blenlib should at least try to be exception safe, so that they can be used if someone wants to use exceptions in some isolated area. This patch improves the exception safety of the Vector, Array and Stack data structure. This is mainly achieved by reordering some lines and doing some explicit exception handling. I don't expect performance of common operations to be affected by this change. The three containers are supposed to provide at least the basic exception guarantee for most methods (except for e.g. `*_unchecked` methods). So, resources should not leak when the contained type throws an exception. I also added new unit tests that test the exception handling in various cases. --- source/blender/blenlib/tests/BLI_stack_cxx_test.cc | 56 ++++++++++++++++++++++ 1 file changed, 56 insertions(+) (limited to 'source/blender/blenlib/tests/BLI_stack_cxx_test.cc') diff --git a/source/blender/blenlib/tests/BLI_stack_cxx_test.cc b/source/blender/blenlib/tests/BLI_stack_cxx_test.cc index 3572e751b88..c03893c5596 100644 --- a/source/blender/blenlib/tests/BLI_stack_cxx_test.cc +++ b/source/blender/blenlib/tests/BLI_stack_cxx_test.cc @@ -1,5 +1,6 @@ /* Apache License, Version 2.0 */ +#include "BLI_exception_safety_test_utils.hh" #include "BLI_stack.hh" #include "BLI_strict_flags.h" #include "BLI_vector.hh" @@ -185,4 +186,59 @@ TEST(stack, OveralignedValues) } } +TEST(stack, SpanConstructorExceptions) +{ + std::array values; + values[3].throw_during_copy = true; + EXPECT_ANY_THROW({ Stack stack(values); }); +} + +TEST(stack, MoveConstructorExceptions) +{ + Stack stack; + stack.push({}); + stack.push({}); + stack.peek().throw_during_move = true; + EXPECT_ANY_THROW({ Stack moved_stack{std::move(stack)}; }); +} + +TEST(stack, PushExceptions) +{ + Stack stack; + stack.push({}); + stack.push({}); + ExceptionThrower *ptr1 = &stack.peek(); + ExceptionThrower value; + value.throw_during_copy = true; + EXPECT_ANY_THROW({ stack.push(value); }); + EXPECT_EQ(stack.size(), 2); + ExceptionThrower *ptr2 = &stack.peek(); + EXPECT_EQ(ptr1, ptr2); + EXPECT_TRUE(stack.is_invariant_maintained()); +} + +TEST(stack, PopExceptions) +{ + Stack stack; + stack.push({}); + stack.peek().throw_during_move = true; + stack.push({}); + stack.pop(); /* NOLINT: bugprone-throw-keyword-missing */ + EXPECT_ANY_THROW({ stack.pop(); }); /* NOLINT: bugprone-throw-keyword-missing */ + EXPECT_EQ(stack.size(), 1); + EXPECT_TRUE(stack.is_invariant_maintained()); +} + +TEST(stack, PushMultipleExceptions) +{ + Stack stack; + stack.push({}); + std::array values; + values[6].throw_during_copy = true; + EXPECT_ANY_THROW({ stack.push_multiple(values); }); + EXPECT_TRUE(stack.is_invariant_maintained()); + EXPECT_ANY_THROW({ stack.push_multiple(values); }); + EXPECT_TRUE(stack.is_invariant_maintained()); +} + } // namespace blender::tests -- cgit v1.2.3