SQL: Make QSqlDatabase::DriverDict creation thread-safe

Make the QSqlDatabase::DriverDict thread-safe and make sure it's
properly cleaned up on destruction.

Fixes: QTBUG-112961
Change-Id: I1ff70e477579231754ef829fdede944d6042894d
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
(cherry picked from commit 048a68c1e965350ad9ff7d4ab1fdaac56a2583a0)
This commit is contained in:
Christian Ehrlicher 2023-04-18 19:40:25 +02:00
parent e403c8d3f2
commit 8d2bdc9cd5
2 changed files with 20 additions and 15 deletions

View File

@ -10,6 +10,7 @@
#include "qsqldriver.h"
#include "qsqldriverplugin.h"
#include "qsqlindex.h"
#include "QtCore/qapplicationstatic.h"
#include "private/qfactoryloader_p.h"
#include "private/qsqlnulldriver_p.h"
#include "qmutex.h"
@ -26,8 +27,6 @@ Q_GLOBAL_STATIC_WITH_ARGS(QFactoryLoader, loader,
const char *QSqlDatabase::defaultConnection = const_cast<char *>("qt_sql_default_connection");
typedef QHash<QString, QSqlDriverCreatorBase*> DriverDict;
class QConnectionDict: public QHash<QString, QSqlDatabase>
{
public:
@ -46,6 +45,14 @@ public:
};
Q_GLOBAL_STATIC(QConnectionDict, dbDict)
namespace {
struct DriverDict : public QHash<QString, QSqlDriverCreatorBase*>
{
~DriverDict();
};
}
Q_APPLICATION_STATIC(DriverDict, qtDriverDict)
class QSqlDatabasePrivate
{
public:
@ -121,23 +128,15 @@ void QSqlDatabasePrivate::cleanConnections()
dict->clear();
}
static bool qDriverDictInit = false;
static void cleanDriverDict()
DriverDict::~DriverDict()
{
qDeleteAll(QSqlDatabasePrivate::driverDict());
QSqlDatabasePrivate::driverDict().clear();
qDeleteAll(*this);
QSqlDatabasePrivate::cleanConnections();
qDriverDictInit = false;
}
DriverDict &QSqlDatabasePrivate::driverDict()
{
static DriverDict dict;
if (!qDriverDictInit) {
qDriverDictInit = true;
qAddPostRoutine(cleanDriverDict);
}
return dict;
return *qtDriverDict();
}
QSqlDatabasePrivate *QSqlDatabasePrivate::shared_null()
@ -511,7 +510,8 @@ QStringList QSqlDatabase::drivers()
list << it.value();
}
DriverDict dict = QSqlDatabasePrivate::driverDict();
QReadLocker locker(&dbDict()->lock);
const DriverDict &dict = QSqlDatabasePrivate::driverDict();
for (DriverDict::const_iterator i = dict.constBegin(); i != dict.constEnd(); ++i) {
if (!list.contains(i.key()))
list << i.key();
@ -649,7 +649,8 @@ void QSqlDatabasePrivate::init(const QString &type)
drvName = type;
if (!driver) {
DriverDict dict = QSqlDatabasePrivate::driverDict();
QReadLocker locker(&dbDict()->lock);
const DriverDict &dict = QSqlDatabasePrivate::driverDict();
for (DriverDict::const_iterator it = dict.constBegin();
it != dict.constEnd() && !driver; ++it) {
if (type == it.key()) {

View File

@ -135,6 +135,10 @@ void tst_QSql::open()
void tst_QSql::openInvalid()
{
int argc = 1;
char *argv[] = { const_cast<char*>(QTest::currentAppName()) };
QCoreApplication app(argc, argv, false);
QSqlDatabase db;
QVERIFY(!db.open());