From 35a53d8f2df4f728f0ba7942571788132bea7a4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=BCnter=20Obiltschnig?= Date: Wed, 16 Jun 2021 08:07:39 +0200 Subject: [PATCH] add comment regarding potential UB in AnyTest::testCastToReference() --- Foundation/testsuite/src/AnyTest.cpp | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/Foundation/testsuite/src/AnyTest.cpp b/Foundation/testsuite/src/AnyTest.cpp index 4cab77b4f..6d8288565 100644 --- a/Foundation/testsuite/src/AnyTest.cpp +++ b/Foundation/testsuite/src/AnyTest.cpp @@ -53,7 +53,7 @@ AnyTest::~AnyTest() void AnyTest::testDefaultCtor() { const Any value; - + assertTrue (value.empty()); assertTrue (0 == AnyCast(&value)); assertTrue (value.type() == typeid(void)); @@ -64,7 +64,7 @@ void AnyTest::testConvertingCtor() { std::string text = "test message"; Any value = text; - + assertTrue (!value.empty()); assertTrue (value.type() == typeid(std::string)); assertTrue (0 == AnyCast(&value)); @@ -78,7 +78,7 @@ void AnyTest::testCopyCtor() { std::string text = "test message"; Any original = text, copy = original; - + assertTrue (!copy.empty()); assertTrue (original.type() == copy.type()); assertTrue (AnyCast(original) == AnyCast(copy)); @@ -92,7 +92,7 @@ void AnyTest::testCopyAssign() std::string text = "test message"; Any original = text, copy; Any* assignResult = &(copy = original); - + assertTrue (!copy.empty()); assertTrue (original.type() == copy.type()); assertTrue (AnyCast(original) == AnyCast(copy)); @@ -114,7 +114,7 @@ void AnyTest::testConvertingAssign() std::string text = "test message"; Any value; Any* assignResult = &(value = text); - + assertTrue (!value.empty()); assertTrue (value.type() == typeid(std::string)); assertTrue (0 == AnyCast(&value)); @@ -129,21 +129,23 @@ void AnyTest::testCastToReference() { Any a(137); const Any b(a); - + int& ra = AnyCast(a); int const& ra_c = AnyCast(a); + // NOTE: The following two AnyCasts will trigger the + // undefined behavior sanitizer. int volatile& ra_v = AnyCast(a); int const volatile& ra_cv = AnyCast(a); - + // cv references to same obj assertTrue (&ra == &ra_c && &ra == &ra_v && &ra == &ra_cv); - + int const & rb_c = AnyCast(b); int const volatile & rb_cv = AnyCast(b); assertTrue (&rb_c == &rb_cv); // cv references to copied const obj assertTrue (&ra != &rb_c); // copies hold different objects - + ++ra; int incremented = AnyCast(a); assertTrue (incremented == 138); // increment by reference changes value @@ -168,7 +170,7 @@ void AnyTest::testBadCast() { std::string text = "test message"; Any value = text; - + try { AnyCast(value); @@ -184,7 +186,7 @@ void AnyTest::testSwap() Any original = text, swapped; std::string* originalPtr = AnyCast(&original); Any* swapResult = &original.swap(swapped); - + assertTrue (original.empty()); assertTrue (!swapped.empty()); assertTrue (swapped.type() == typeid(std::string)); @@ -202,7 +204,7 @@ void AnyTest::testEmptyCopy() const Any null; Any copied = null, assigned; assigned = null; - + assertTrue (null.empty()); assertTrue (copied.empty()); assertTrue (assigned.empty()); @@ -261,7 +263,7 @@ void AnyTest::testVector() assertTrue (tmp2.size() == 3); const std::vector& vecCRef = RefAnyCast >(a); std::vector& vecRef = RefAnyCast >(a); - + assertTrue (vecRef[0] == 1); assertTrue (vecRef[1] == 2); assertTrue (vecRef[2] == 3);