aboutsummaryrefslogtreecommitdiff
path: root/lib/jobs
diff options
context:
space:
mode:
authorKitsune Ral <Kitsune-Ral@users.sf.net>2020-12-27 22:24:48 +0100
committerGitHub <noreply@github.com>2020-12-27 22:24:48 +0100
commit95343a29f141f7d2b231a41615bc0e57acb890fe (patch)
tree96879173cdc53b17f0a7994ac8ea9a70757503fc /lib/jobs
parentd5670a9dea90e111d805ae144c7295cd58b29d22 (diff)
parent9d854e778d8d6ef8e03e1ea74fe958675b24fd45 (diff)
downloadlibquotient-95343a29f141f7d2b231a41615bc0e57acb890fe.tar.gz
libquotient-95343a29f141f7d2b231a41615bc0e57acb890fe.zip
Merge pull request #430 from nicolasfella/replycleanup
Fix use-after-free of QNetworkReply in BaseJob
Diffstat (limited to 'lib/jobs')
-rw-r--r--lib/jobs/basejob.cpp33
1 files changed, 19 insertions, 14 deletions
diff --git a/lib/jobs/basejob.cpp b/lib/jobs/basejob.cpp
index 3fa1cd94..2ac942f5 100644
--- a/lib/jobs/basejob.cpp
+++ b/lib/jobs/basejob.cpp
@@ -24,6 +24,7 @@
#include <QtCore/QTimer>
#include <QtCore/QStringBuilder>
#include <QtCore/QMetaEnum>
+#include <QtCore/QPointer>
#include <QtNetwork/QNetworkAccessManager>
#include <QtNetwork/QNetworkReply>
#include <QtNetwork/QNetworkRequest>
@@ -76,15 +77,6 @@ QDebug BaseJob::Status::dumpToLog(QDebug dbg) const
return dbg << ": " << message;
}
-struct NetworkReplyDeleter : public QScopedPointerDeleteLater {
- static inline void cleanup(QNetworkReply* reply)
- {
- if (reply && reply->isRunning())
- reply->abort();
- QScopedPointerDeleteLater::cleanup(reply);
- }
-};
-
template <typename... Ts>
constexpr auto make_array(Ts&&... items)
{
@@ -112,6 +104,16 @@ public:
retryTimer.setSingleShot(true);
}
+ ~Private()
+ {
+ if (reply) {
+ if (reply->isRunning()) {
+ reply->abort();
+ }
+ delete reply;
+ }
+ }
+
void sendRequest();
/*! \brief Parse the response byte array into JSON
*
@@ -140,7 +142,10 @@ public:
QByteArrayList expectedKeys;
- QScopedPointer<QNetworkReply, NetworkReplyDeleter> reply;
+ // When the QNetworkAccessManager is destroyed it destroys all pending replies.
+ // Using QPointer allows us to know when that happend.
+ QPointer<QNetworkReply> reply;
+
Status status = Unprepared;
QByteArray rawResponse;
/// Contains a null document in case of non-JSON body (for a successful
@@ -315,16 +320,16 @@ void BaseJob::Private::sendRequest()
switch (verb) {
case HttpVerb::Get:
- reply.reset(connection->nam()->get(req));
+ reply = connection->nam()->get(req);
break;
case HttpVerb::Post:
- reply.reset(connection->nam()->post(req, requestData.source()));
+ reply = connection->nam()->post(req, requestData.source());
break;
case HttpVerb::Put:
- reply.reset(connection->nam()->put(req, requestData.source()));
+ reply = connection->nam()->put(req, requestData.source());
break;
case HttpVerb::Delete:
- reply.reset(connection->nam()->sendCustomRequest(req, "DELETE", requestData.source()));
+ reply = connection->nam()->sendCustomRequest(req, "DELETE", requestData.source());
break;
}
}