From fb55e1e71e3c98aca880c23d97f9e811e2b406a0 Mon Sep 17 00:00:00 2001 From: Alexandru Croitor Date: Tue, 30 Jun 2020 18:49:57 +0200 Subject: Sql: Fix heap-user-after-free for globally initialized db objects Becaues the database objects were created as globals, there was a possible use-after-free issue when deleting the objects on application exit. Move the initialization of the database objects into static variables inside the test constructor. As a drive-by, also add one missing test to the CMake projects. Fixes: QTBUG-85357 Change-Id: I2c8f2c5daee96bb9d1d21dae37950a2da5ffdf27 Reviewed-by: Joerg Bornemann --- .../auto/sql/models/qsqltablemodel/tst_qsqltablemodel.cpp | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) (limited to 'tests/auto/sql/models/qsqltablemodel/tst_qsqltablemodel.cpp') diff --git a/tests/auto/sql/models/qsqltablemodel/tst_qsqltablemodel.cpp b/tests/auto/sql/models/qsqltablemodel/tst_qsqltablemodel.cpp index 44dd4a74cf..81dcaa0975 100644 --- a/tests/auto/sql/models/qsqltablemodel/tst_qsqltablemodel.cpp +++ b/tests/auto/sql/models/qsqltablemodel/tst_qsqltablemodel.cpp @@ -34,9 +34,9 @@ #include #include -const QString test(qTableName("test", __FILE__, QSqlDatabase())), - test2(qTableName("test2", __FILE__, QSqlDatabase())), - test3(qTableName("test3", __FILE__, QSqlDatabase())); +QString test; +QString test2; +QString test3; // In order to catch when the warning message occurs, indicating that the database belongs to another // thread, we have to install our own message handler. To ensure that the test reporting still happens @@ -164,6 +164,15 @@ private: tst_QSqlTableModel::tst_QSqlTableModel() { + static QSqlDatabase static_qtest_db_1 = QSqlDatabase(); + test = qTableName("test1", __FILE__, static_qtest_db_1); + + static QSqlDatabase static_qtest_db_2 = QSqlDatabase(); + test2 = qTableName("test2", __FILE__, static_qtest_db_2); + + static QSqlDatabase static_qtest_db_3 = QSqlDatabase(); + test3 = qTableName("test3", __FILE__, static_qtest_db_3); + QVERIFY(dbs.open()); } -- cgit v1.2.3