tst_qobjectrace: fix potential UB (something with pointers and new)

The current [basic.life] wording seems to cover the existing code, but
IIRC, older versions of [basic.life] were not so relaxed. In
particular, while not completely pertinent, the second placement new
is awfully similar to http://eel.is/c++draft/ptr.launder#example-1

Just make all of this SEP and use std::optional. That way, the code
gets simpler, too, plus we get rid of the last use of C++23-deprecated
std::aligned_storage.

The reset() before the 2nd emplace() isn't necessary, but, in a test,
it doesn't hurt, either, and keeps code readers from guessing whether
the first-emplaced object's dtor is actually properly run (it is).

Pick-to: 6.3 6.2
Fixes: QTBUG-99122
Change-Id: If31a46f8be3a74499f1176133029d097faf7dfe9
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Marc Mutz 2022-05-07 13:30:31 +02:00
parent 32f1847e1e
commit 277c23956c

View File

@ -32,6 +32,8 @@
#include <QtTest/private/qemulationdetector_p.h>
#include <optional>
enum { OneMinute = 60 * 1000,
TwoMinutes = OneMinute * 2 };
@ -347,17 +349,13 @@ void tst_QObjectRace::blockingQueuedDestroyRace()
while (iteration++ < MinIterations || !timer.hasExpired()) {
// Manually allocate some storage, and create a receiver in there
std::aligned_storage<
sizeof(BlockingQueuedDestroyRaceObject),
alignof(BlockingQueuedDestroyRaceObject)
>::type storage;
std::optional<BlockingQueuedDestroyRaceObject> receiver;
auto *receiver = reinterpret_cast<BlockingQueuedDestroyRaceObject *>(&storage);
new (receiver) BlockingQueuedDestroyRaceObject(BlockingQueuedDestroyRaceObject::Behavior::Normal);
receiver.emplace(BlockingQueuedDestroyRaceObject::Behavior::Normal);
// Connect it to the sender via BlockingQueuedConnection
QVERIFY(connect(&sender, &BlockingQueuedDestroyRaceObject::aSignal,
receiver, &BlockingQueuedDestroyRaceObject::aSlot,
&*receiver, &BlockingQueuedDestroyRaceObject::aSlot,
Qt::BlockingQueuedConnection));
const auto emitUntilDestroyed = [&sender] {
@ -379,15 +377,13 @@ void tst_QObjectRace::blockingQueuedDestroyRace()
// - the metacall event to be posted to a destroyed object;
// - the metacall event to be posted to the wrong object.
// In both cases we hope to catch the race by crashing.
receiver->~BlockingQueuedDestroyRaceObject();
new (receiver) BlockingQueuedDestroyRaceObject(BlockingQueuedDestroyRaceObject::Behavior::Crash);
receiver.reset();
receiver.emplace(BlockingQueuedDestroyRaceObject::Behavior::Crash);
// Flush events
QTest::qWait(0);
thread->wait();
receiver->~BlockingQueuedDestroyRaceObject();
}
#endif
}