QFileSystemModel: fix leak in unittest detected by ASAN

Amends e524724f9dc6f64f6f94b842682c751dcd07225f.

Indirect leak of 24 byte(s) in 1 object(s) allocated from:
    #0 0x7f73b50fc1d8 in operator new(unsigned long) (/lib64/libasan.so.8+0xfc1d8) (BuildId: 1827a4c72065a9f25ba519b25166029eebbf519f)
    #1 0x7f73b4cc18d4 in std::__detail::_MakeUniq<QSignalSpyPrivate>::__single_object std::make_unique<QSignalSpyPrivate, QSignalSpy*>(QSignalSpy*&&) /usr/include/c++/13/bits/unique_ptr.h:1070
    #2 0x7f73b4cbf0a4 in QSignalSpy::QSignalSpy(QSignalSpy::ObjectSignal) src/testlib/qsignalspy.cpp:259
    #3 0x55c0e313cd7d in QSignalSpy::QSignalSpy<void (QAbstractItemModel::*)(QModelIndex const&, int, int, QAbstractItemModel::QPrivateSignal)>(QtPrivate::FunctionPointer<void (QAbstractItemModel::*)(QModelIndex const&, int, int, QAbstractItemModel::QPrivateSignal)>::Object const*, void (QAbstractItemModel::*)(QModelIndex const&, int, int, QAbstractItemModel::QPrivateSignal)) (build/tests/auto/gui/itemmodels/qfilesystemmodel/tst_qfilesystemmodel+0x83d7d) (BuildId: b2e416794e25fdb925a191cc4a5efe3cb04efb0e)
    #4 0x55c0e30eccac in tst_QFileSystemModel::prepareTestModelRoot(QFileSystemModel*, QString const&, QSignalSpy**, QSignalSpy**) tests/auto/gui/itemmodels/qfilesystemmodel/tst_qfilesystemmodel.cpp:392
    #5 0x55c0e30edacf in tst_QFileSystemModel::rowCount() tests/auto/gui/itemmodels/qfilesystemmodel/tst_qfilesystemmodel.cpp:420

Pick-to: 6.7 6.5 6.2
Change-Id: Ia085f9235f3bec252290f4fbe5ea0958f91a5e5a
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
(cherry picked from commit 5686af295c98a5ed4b0793ad0098cef76f39a2d7)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Ahmad Samir 2024-06-09 00:04:47 +03:00 committed by Qt Cherry-pick Bot
parent b1301fffe9
commit 741c1d0f11

View File

@ -117,8 +117,7 @@ protected:
bool createFiles(QFileSystemModel *model, const QString &test_path,
const QStringList &initial_files, int existingFileCount = 0,
const QStringList &initial_dirs = QStringList());
QModelIndex prepareTestModelRoot(QFileSystemModel *model, const QString &test_path,
QSignalSpy **spy2 = nullptr, QSignalSpy **spy3 = nullptr);
QModelIndex prepareTestModelRoot(QFileSystemModel *model, const QString &test_path);
private:
QString flatDirTestPath;
@ -382,17 +381,12 @@ bool tst_QFileSystemModel::createFiles(QFileSystemModel *model, const QString &t
return true;
}
QModelIndex tst_QFileSystemModel::prepareTestModelRoot(QFileSystemModel *model, const QString &test_path,
QSignalSpy **spy2, QSignalSpy **spy3)
QModelIndex tst_QFileSystemModel::prepareTestModelRoot(QFileSystemModel *model,
const QString &test_path)
{
if (model->rowCount(model->index(test_path)) != 0)
return QModelIndex();
if (spy2)
*spy2 = new QSignalSpy(model, &QFileSystemModel::rowsInserted);
if (spy3)
*spy3 = new QSignalSpy(model, &QFileSystemModel::rowsAboutToBeInserted);
QStringList files = { "b", "d", "f", "h", "j", ".a", ".c", ".e", ".g" };
if (!createFiles(model, test_path, files))
@ -412,16 +406,16 @@ QModelIndex tst_QFileSystemModel::prepareTestModelRoot(QFileSystemModel *model,
void tst_QFileSystemModel::rowCount()
{
QSignalSpy *spy2 = nullptr;
QSignalSpy *spy3 = nullptr;
QScopedPointer<QFileSystemModel> model(new QFileSystemModel);
QAbstractItemModelTester tester(model.get());
QSignalSpy rowsInsertedSpy(model.get(), &QFileSystemModel::rowsInserted);
QSignalSpy rowsAboutToBeInsertedSpy(model.get(), &QFileSystemModel::rowsAboutToBeInserted);
tester.setUseFetchMore(false);
QModelIndex root = prepareTestModelRoot(model.data(), flatDirTestPath, &spy2, &spy3);
QModelIndex root = prepareTestModelRoot(model.data(), flatDirTestPath);
QVERIFY(root.isValid());
QVERIFY(spy2 && spy2->size() > 0);
QVERIFY(spy3 && spy3->size() > 0);
QCOMPARE_GT(rowsInsertedSpy.size(), 0);
QCOMPARE_GT(rowsAboutToBeInsertedSpy.size(), 0);
}
void tst_QFileSystemModel::rowsInserted_data()