add comment regarding potential UB in AnyTest::testCastToReference()

This commit is contained in:
Günter Obiltschnig 2021-06-16 08:07:39 +02:00
parent 5948cc8be4
commit 35a53d8f2d

View File

@ -53,7 +53,7 @@ AnyTest::~AnyTest()
void AnyTest::testDefaultCtor() void AnyTest::testDefaultCtor()
{ {
const Any value; const Any value;
assertTrue (value.empty()); assertTrue (value.empty());
assertTrue (0 == AnyCast<int>(&value)); assertTrue (0 == AnyCast<int>(&value));
assertTrue (value.type() == typeid(void)); assertTrue (value.type() == typeid(void));
@ -64,7 +64,7 @@ void AnyTest::testConvertingCtor()
{ {
std::string text = "test message"; std::string text = "test message";
Any value = text; Any value = text;
assertTrue (!value.empty()); assertTrue (!value.empty());
assertTrue (value.type() == typeid(std::string)); assertTrue (value.type() == typeid(std::string));
assertTrue (0 == AnyCast<int>(&value)); assertTrue (0 == AnyCast<int>(&value));
@ -78,7 +78,7 @@ void AnyTest::testCopyCtor()
{ {
std::string text = "test message"; std::string text = "test message";
Any original = text, copy = original; Any original = text, copy = original;
assertTrue (!copy.empty()); assertTrue (!copy.empty());
assertTrue (original.type() == copy.type()); assertTrue (original.type() == copy.type());
assertTrue (AnyCast<std::string>(original) == AnyCast<std::string>(copy)); assertTrue (AnyCast<std::string>(original) == AnyCast<std::string>(copy));
@ -92,7 +92,7 @@ void AnyTest::testCopyAssign()
std::string text = "test message"; std::string text = "test message";
Any original = text, copy; Any original = text, copy;
Any* assignResult = &(copy = original); Any* assignResult = &(copy = original);
assertTrue (!copy.empty()); assertTrue (!copy.empty());
assertTrue (original.type() == copy.type()); assertTrue (original.type() == copy.type());
assertTrue (AnyCast<std::string>(original) == AnyCast<std::string>(copy)); assertTrue (AnyCast<std::string>(original) == AnyCast<std::string>(copy));
@ -114,7 +114,7 @@ void AnyTest::testConvertingAssign()
std::string text = "test message"; std::string text = "test message";
Any value; Any value;
Any* assignResult = &(value = text); Any* assignResult = &(value = text);
assertTrue (!value.empty()); assertTrue (!value.empty());
assertTrue (value.type() == typeid(std::string)); assertTrue (value.type() == typeid(std::string));
assertTrue (0 == AnyCast<int>(&value)); assertTrue (0 == AnyCast<int>(&value));
@ -129,21 +129,23 @@ void AnyTest::testCastToReference()
{ {
Any a(137); Any a(137);
const Any b(a); const Any b(a);
int& ra = AnyCast<int &>(a); int& ra = AnyCast<int &>(a);
int const& ra_c = AnyCast<int const &>(a); int const& ra_c = AnyCast<int const &>(a);
// NOTE: The following two AnyCasts will trigger the
// undefined behavior sanitizer.
int volatile& ra_v = AnyCast<int volatile &>(a); int volatile& ra_v = AnyCast<int volatile &>(a);
int const volatile& ra_cv = AnyCast<int const volatile&>(a); int const volatile& ra_cv = AnyCast<int const volatile&>(a);
// cv references to same obj // cv references to same obj
assertTrue (&ra == &ra_c && &ra == &ra_v && &ra == &ra_cv); assertTrue (&ra == &ra_c && &ra == &ra_v && &ra == &ra_cv);
int const & rb_c = AnyCast<int const &>(b); int const & rb_c = AnyCast<int const &>(b);
int const volatile & rb_cv = AnyCast<int const volatile &>(b); int const volatile & rb_cv = AnyCast<int const volatile &>(b);
assertTrue (&rb_c == &rb_cv); // cv references to copied const obj assertTrue (&rb_c == &rb_cv); // cv references to copied const obj
assertTrue (&ra != &rb_c); // copies hold different objects assertTrue (&ra != &rb_c); // copies hold different objects
++ra; ++ra;
int incremented = AnyCast<int>(a); int incremented = AnyCast<int>(a);
assertTrue (incremented == 138); // increment by reference changes value assertTrue (incremented == 138); // increment by reference changes value
@ -168,7 +170,7 @@ void AnyTest::testBadCast()
{ {
std::string text = "test message"; std::string text = "test message";
Any value = text; Any value = text;
try try
{ {
AnyCast<const char *>(value); AnyCast<const char *>(value);
@ -184,7 +186,7 @@ void AnyTest::testSwap()
Any original = text, swapped; Any original = text, swapped;
std::string* originalPtr = AnyCast<std::string>(&original); std::string* originalPtr = AnyCast<std::string>(&original);
Any* swapResult = &original.swap(swapped); Any* swapResult = &original.swap(swapped);
assertTrue (original.empty()); assertTrue (original.empty());
assertTrue (!swapped.empty()); assertTrue (!swapped.empty());
assertTrue (swapped.type() == typeid(std::string)); assertTrue (swapped.type() == typeid(std::string));
@ -202,7 +204,7 @@ void AnyTest::testEmptyCopy()
const Any null; const Any null;
Any copied = null, assigned; Any copied = null, assigned;
assigned = null; assigned = null;
assertTrue (null.empty()); assertTrue (null.empty());
assertTrue (copied.empty()); assertTrue (copied.empty());
assertTrue (assigned.empty()); assertTrue (assigned.empty());
@ -261,7 +263,7 @@ void AnyTest::testVector()
assertTrue (tmp2.size() == 3); assertTrue (tmp2.size() == 3);
const std::vector<int>& vecCRef = RefAnyCast<std::vector<int> >(a); const std::vector<int>& vecCRef = RefAnyCast<std::vector<int> >(a);
std::vector<int>& vecRef = RefAnyCast<std::vector<int> >(a); std::vector<int>& vecRef = RefAnyCast<std::vector<int> >(a);
assertTrue (vecRef[0] == 1); assertTrue (vecRef[0] == 1);
assertTrue (vecRef[1] == 2); assertTrue (vecRef[1] == 2);
assertTrue (vecRef[2] == 3); assertTrue (vecRef[2] == 3);