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()
{
const Any value;
assertTrue (value.empty());
assertTrue (0 == AnyCast<int>(&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<int>(&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<std::string>(original) == AnyCast<std::string>(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<std::string>(original) == AnyCast<std::string>(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<int>(&value));
@ -129,21 +129,23 @@ void AnyTest::testCastToReference()
{
Any a(137);
const Any b(a);
int& ra = AnyCast<int &>(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 const volatile& ra_cv = AnyCast<int const volatile&>(a);
// cv references to same obj
assertTrue (&ra == &ra_c && &ra == &ra_v && &ra == &ra_cv);
int const & rb_c = AnyCast<int const &>(b);
int const volatile & rb_cv = AnyCast<int const volatile &>(b);
assertTrue (&rb_c == &rb_cv); // cv references to copied const obj
assertTrue (&ra != &rb_c); // copies hold different objects
++ra;
int incremented = AnyCast<int>(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<const char *>(value);
@ -184,7 +186,7 @@ void AnyTest::testSwap()
Any original = text, swapped;
std::string* originalPtr = AnyCast<std::string>(&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<int>& vecCRef = RefAnyCast<std::vector<int> >(a);
std::vector<int>& vecRef = RefAnyCast<std::vector<int> >(a);
assertTrue (vecRef[0] == 1);
assertTrue (vecRef[1] == 2);
assertTrue (vecRef[2] == 3);