From: APTX Date: Mon, 15 Apr 2013 14:28:15 +0000 (+0200) Subject: Fix possible crash in Database::exec(). X-Git-Url: https://gitweb.tyo.aptx.org/?a=commitdiff_plain;h=8dbf679930be8c49587ad01131f1ec632c047660;p=localmylist.git Fix possible crash in Database::exec(). Disconnecting clears all prepared statements so a query passed to exec might would invalid. --- diff --git a/localmylist/database.cpp b/localmylist/database.cpp index aba3988..a74c4c7 100644 --- a/localmylist/database.cpp +++ b/localmylist/database.cpp @@ -7,6 +7,8 @@ #include #include #include +#include + #include namespace LocalMyList { @@ -1701,13 +1703,22 @@ bool Database::exec(QSqlQuery &query) { Q_ASSERT_X(d->thread == QThread::currentThread(), "threads", "DB used from different thread"); - if (!d->db.isOpen()) + if (!isConnected()) { - disconnect(); - if (!connect()) - return false; + auto keyIt = std::find_if(d->preparedQueries.constBegin(), d->preparedQueries.constEnd(), + [&](const QSqlQuery &a) { return &a == &query;}); - return retryExec(query, true); + Q_ASSERT(keyIt != d->preparedQueries.constEnd()); + + auto key = keyIt.key(); + auto values = query.boundValues(); + + query = prepare(key); + + for(auto it = values.constBegin(); it != values.constEnd(); ++it) + query.bindValue(it.key(), it.value()); + + return retryExec(query, false); } if (!query.exec()) @@ -1719,7 +1730,7 @@ bool Database::exec(const QString &sql) { Q_ASSERT_X(d->thread == QThread::currentThread(), "threads", "DB used from different thread"); - if (!d->db.isOpen()) + if (!isConnected()) { disconnect(); if (!connect())