File indexing completed on 2024-11-10 04:40:12

0001 /*
0002     SPDX-FileCopyrightText: 2023 Daniel Vrátil <dvratil@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "itemchangelogtest.h"
0008 #include "item_p.h"
0009 #include "itemchangelog_p.h"
0010 
0011 #include <QTest>
0012 
0013 #include <type_traits>
0014 
0015 using namespace Akonadi;
0016 
0017 void ItemChangelogTest::testDoesntCrashWhenReassigning()
0018 {
0019     auto *log = ItemChangeLog::instance();
0020 
0021     // Make the keys non-contiguous so we can trigger rehashing in the next loop
0022     for (int i = 0; i < 50; i += 2) {
0023         // We can reinrepret_cast because ItemPrivate pointer is used only
0024         // as an opaque key in the hash table.
0025         log->addedFlags(reinterpret_cast<ItemPrivate *>(i)) = {QByteArray("foo")};
0026     }
0027 
0028     // We will be inserting in between existing items, which will eventually trigger
0029     // rehashing
0030     for (int i = 1; i < 50; i += 2) {
0031         // Previously this would crash because addedFlags() would return a reference
0032         // to a valud stored in the hash table, which would be invalidated by rehashing
0033         // caused by the call to left-hand-side addedFlags().
0034         log->addedFlags(reinterpret_cast<ItemPrivate *>(i)) = log->addedFlags(reinterpret_cast<const ItemPrivate *>(i - 1));
0035     }
0036 }
0037 
0038 QTEST_GUILESS_MAIN(ItemChangelogTest);
0039 
0040 #include "moc_itemchangelogtest.cpp"