Code quality updates for 2.4.0 (#2709)

* Minor code quality fixes found by Codacy
* Fix unused variables when WITH_XC_NETWORKING is OFF
* Fix #2684, resolve entry references from the root group
* Fix #2697 and Fix #2699, listen specifically for 
WM_QUERYENDSESSION and WM_ENDSESSION on 
Windows to gracefully shutdown KeePassXC
* Cleanup proxy code and add explicit closure for
shutdown messages
This commit is contained in:
Jonathan White 2019-02-18 08:26:56 -05:00 committed by GitHub
parent fa3c959212
commit 0c587999c6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 97 additions and 46 deletions

View File

@ -53,7 +53,7 @@ protected slots:
protected: protected:
virtual void readLength() = 0; virtual void readLength() = 0;
virtual bool readStdIn(const quint32 length) = 0; virtual bool readStdIn(const quint32 length) = 0;
void readNativeMessages(); virtual void readNativeMessages();
QString jsonToString(const QJsonObject& json) const; QString jsonToString(const QJsonObject& json) const;
void sendReply(const QJsonObject& json); void sendReply(const QJsonObject& json);
void sendReply(const QString& reply); void sendReply(const QString& reply);

View File

@ -32,7 +32,7 @@ class NativeMessagingHost : public NativeMessagingBase
public: public:
explicit NativeMessagingHost(DatabaseTabWidget* parent = nullptr, const bool enabled = false); explicit NativeMessagingHost(DatabaseTabWidget* parent = nullptr, const bool enabled = false);
~NativeMessagingHost(); ~NativeMessagingHost() override;
int init(); int init();
void run(); void run();
void stop(); void stop();

View File

@ -926,7 +926,7 @@ QString Entry::resolveReferencePlaceholderRecursive(const QString& placeholder,
Q_ASSERT(m_group); Q_ASSERT(m_group);
Q_ASSERT(m_group->database()); Q_ASSERT(m_group->database());
const Entry* refEntry = m_group->findEntryBySearchTerm(searchText, searchInType); const Entry* refEntry = m_group->database()->rootGroup()->findEntryBySearchTerm(searchText, searchInType);
if (refEntry) { if (refEntry) {
const QString wantedField = match.captured(EntryAttributes::WantedFieldGroupName); const QString wantedField = match.captured(EntryAttributes::WantedFieldGroupName);

View File

@ -1,8 +1,30 @@
/*
* Copyright (C) 2013 Felix Geyer <debfx@fobos.de>
* Copyright (C) 2018 KeePassXC Team <team@keepassxc.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "OSEventFilter.h" #include "OSEventFilter.h"
#include <QByteArray> #include <QByteArray>
#include "gui/MainWindow.h"
#include "autotype/AutoType.h" #include "autotype/AutoType.h"
#ifdef Q_OS_WIN
#include <windows.h>
#endif
OSEventFilter::OSEventFilter() OSEventFilter::OSEventFilter()
{ {
@ -15,12 +37,18 @@ bool OSEventFilter::nativeEventFilter(const QByteArray& eventType, void* message
#if defined(Q_OS_UNIX) #if defined(Q_OS_UNIX)
if (eventType == QByteArrayLiteral("xcb_generic_event_t")) { if (eventType == QByteArrayLiteral("xcb_generic_event_t")) {
#elif defined(Q_OS_WIN) #elif defined(Q_OS_WIN)
if (eventType == QByteArrayLiteral("windows_generic_MSG") auto winmsg = static_cast<MSG*>(message);
|| eventType == QByteArrayLiteral("windows_dispatcher_MSG")) { if (winmsg->message == WM_QUERYENDSESSION) {
*result = 1;
return true;
} else if (winmsg->message == WM_ENDSESSION) {
getMainWindow()->appExit();
*result = 0;
return true;
} else if (eventType == QByteArrayLiteral("windows_generic_MSG")
|| eventType == QByteArrayLiteral("windows_dispatcher_MSG")) {
#endif #endif
int retCode = autoType()->callEventFilter(message); return autoType()->callEventFilter(message) == 1;
return retCode == 1;
} }
return false; return false;

View File

@ -1,3 +1,21 @@
/*
* Copyright (C) 2013 Felix Geyer <debfx@fobos.de>
* Copyright (C) 2018 KeePassXC Team <team@keepassxc.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef OSEVENTFILTER_H #ifndef OSEVENTFILTER_H
#define OSEVENTFILTER_H #define OSEVENTFILTER_H
#include <QAbstractNativeEventFilter> #include <QAbstractNativeEventFilter>

View File

@ -84,7 +84,8 @@ int Kdf::benchmark(int msec) const
} }
Kdf::BenchmarkThread::BenchmarkThread(int msec, const Kdf* kdf) Kdf::BenchmarkThread::BenchmarkThread(int msec, const Kdf* kdf)
: m_msec(msec) : m_rounds(1)
, m_msec(msec)
, m_kdf(kdf) , m_kdf(kdf)
{ {
} }

View File

@ -19,12 +19,6 @@
#include "BinaryStream.h" #include "BinaryStream.h"
#include <QtEndian> #include <QtEndian>
BinaryStream::BinaryStream(QObject* parent)
: QObject(parent)
, m_timeout(-1)
{
}
BinaryStream::BinaryStream(QIODevice* device) BinaryStream::BinaryStream(QIODevice* device)
: QObject(device) : QObject(device)
, m_timeout(-1) , m_timeout(-1)
@ -36,7 +30,10 @@ BinaryStream::BinaryStream(QByteArray* ba, QObject* parent)
: QObject(parent) : QObject(parent)
, m_timeout(-1) , m_timeout(-1)
{ {
setData(ba); m_buffer.reset(new QBuffer(ba));
m_buffer->open(QIODevice::ReadWrite);
m_device = m_buffer.data();
} }
BinaryStream::~BinaryStream() BinaryStream::~BinaryStream()
@ -53,19 +50,6 @@ QIODevice* BinaryStream::device() const
return m_device; return m_device;
} }
void BinaryStream::setDevice(QIODevice* device)
{
m_device = device;
}
void BinaryStream::setData(QByteArray* ba)
{
m_buffer.reset(new QBuffer(ba));
m_buffer->open(QIODevice::ReadWrite);
m_device = m_buffer.data();
}
void BinaryStream::setTimeout(int timeout) void BinaryStream::setTimeout(int timeout)
{ {
m_timeout = timeout; m_timeout = timeout;

View File

@ -26,16 +26,14 @@
class BinaryStream : QObject class BinaryStream : QObject
{ {
Q_OBJECT Q_OBJECT
Q_DISABLE_COPY(BinaryStream)
public: public:
BinaryStream(QObject* parent = nullptr); explicit BinaryStream(QIODevice* device);
BinaryStream(QIODevice* device); explicit BinaryStream(QByteArray* ba, QObject* parent = nullptr);
BinaryStream(QByteArray* ba, QObject* parent = nullptr); ~BinaryStream() override;
~BinaryStream();
const QString errorString() const; const QString errorString() const;
QIODevice* device() const; QIODevice* device() const;
void setDevice(QIODevice* device);
void setData(QByteArray* ba);
void setTimeout(int timeout); void setTimeout(int timeout);
bool read(QByteArray& ba); bool read(QByteArray& ba);

View File

@ -76,7 +76,7 @@
class BrowserPlugin : public ISettingsPage class BrowserPlugin : public ISettingsPage
{ {
public: public:
BrowserPlugin(DatabaseTabWidget* tabWidget) explicit BrowserPlugin(DatabaseTabWidget* tabWidget)
{ {
m_nativeMessagingHost = m_nativeMessagingHost =
QSharedPointer<NativeMessagingHost>(new NativeMessagingHost(tabWidget, browserSettings()->isEnabled())); QSharedPointer<NativeMessagingHost>(new NativeMessagingHost(tabWidget, browserSettings()->isEnabled()));
@ -713,6 +713,10 @@ void MainWindow::hasUpdateAvailable(bool hasUpdate, const QString& version, bool
updateCheckDialog->showUpdateCheckResponse(hasUpdate, version); updateCheckDialog->showUpdateCheckResponse(hasUpdate, version);
updateCheckDialog->show(); updateCheckDialog->show();
} }
#else
Q_UNUSED(hasUpdate)
Q_UNUSED(version)
Q_UNUSED(isManuallyRequested)
#endif #endif
} }

View File

@ -787,7 +787,7 @@ ShareObserver::Result::Result(const QString& path, ShareObserver::Result::Type t
bool ShareObserver::Result::isValid() const bool ShareObserver::Result::isValid() const
{ {
return !path.isEmpty() || !message.isEmpty() || !message.isEmpty() || !message.isEmpty(); return !path.isEmpty() || !message.isEmpty();
} }
bool ShareObserver::Result::isError() const bool ShareObserver::Result::isError() const

View File

@ -19,7 +19,7 @@
#include <QCoreApplication> #include <QCoreApplication>
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
#include <Winsock2.h> #include <winsock2.h>
#endif #endif
NativeMessagingHost::NativeMessagingHost() NativeMessagingHost::NativeMessagingHost()
@ -36,14 +36,12 @@ NativeMessagingHost::NativeMessagingHost()
} }
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
m_running.store(true); m_running.store(true);
m_future = m_future = QtConcurrent::run(this, &NativeMessagingHost::readNativeMessages);
QtConcurrent::run(this, static_cast<void (NativeMessagingHost::*)()>(&NativeMessagingHost::readNativeMessages));
#endif #endif
connect(m_localSocket, SIGNAL(readyRead()), this, SLOT(newLocalMessage())); connect(m_localSocket, SIGNAL(readyRead()), this, SLOT(newLocalMessage()));
connect(m_localSocket, SIGNAL(disconnected()), this, SLOT(deleteSocket())); connect(m_localSocket, SIGNAL(disconnected()), this, SLOT(deleteSocket()));
connect(m_localSocket, connect(m_localSocket,
SIGNAL(stateChanged(QLocalSocket::LocalSocketState)), SIGNAL(stateChanged(QLocalSocket::LocalSocketState)),
this,
SLOT(socketStateChanged(QLocalSocket::LocalSocketState))); SLOT(socketStateChanged(QLocalSocket::LocalSocketState)));
} }

View File

@ -25,7 +25,7 @@ class NativeMessagingHost : public NativeMessagingBase
Q_OBJECT Q_OBJECT
public: public:
NativeMessagingHost(); NativeMessagingHost();
~NativeMessagingHost(); ~NativeMessagingHost() override;
public slots: public slots:
void newLocalMessage(); void newLocalMessage();
@ -33,12 +33,14 @@ public slots:
void socketStateChanged(QLocalSocket::LocalSocketState socketState); void socketStateChanged(QLocalSocket::LocalSocketState socketState);
private: private:
void readNativeMessages(); void readNativeMessages() override;
void readLength(); void readLength() override;
bool readStdIn(const quint32 length); bool readStdIn(const quint32 length) override;
private: private:
QLocalSocket* m_localSocket; QLocalSocket* m_localSocket;
Q_DISABLE_COPY(NativeMessagingHost)
}; };
#endif // NATIVEMESSAGINGHOST_H #endif // NATIVEMESSAGINGHOST_H

View File

@ -55,13 +55,29 @@ void catchUnixSignals(std::initializer_list<int> quitSignals)
sigaction(sig, &sa, nullptr); sigaction(sig, &sa, nullptr);
} }
} }
#else
#include <windows.h>
BOOL WINAPI ConsoleHandler(DWORD dwType)
{
switch (dwType) {
case CTRL_C_EVENT:
case CTRL_SHUTDOWN_EVENT:
case CTRL_LOGOFF_EVENT:
QCoreApplication::quit();
break;
}
return TRUE;
}
#endif #endif
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
QCoreApplication a(argc, argv); QCoreApplication a(argc, argv);
#if defined(Q_OS_UNIX) || defined(Q_OS_LINUX) #ifndef Q_OS_WIN
catchUnixSignals({SIGQUIT, SIGINT, SIGTERM, SIGHUP}); catchUnixSignals({SIGQUIT, SIGINT, SIGTERM, SIGHUP});
#else
SetConsoleCtrlHandler(static_cast<PHANDLER_ROUTINE>(ConsoleHandler),TRUE);
#endif #endif
NativeMessagingHost host; NativeMessagingHost host;
return a.exec(); return a.exec();

View File

@ -28,6 +28,7 @@ SymmetricCipherStream::SymmetricCipherStream(QIODevice* baseDevice,
, m_error(false) , m_error(false)
, m_isInitialized(false) , m_isInitialized(false)
, m_dataWritten(false) , m_dataWritten(false)
, m_streamCipher(false)
{ {
} }

View File

@ -28,6 +28,7 @@ UpdateChecker::UpdateChecker(QObject* parent)
: QObject(parent) : QObject(parent)
, m_netMgr(new QNetworkAccessManager(this)) , m_netMgr(new QNetworkAccessManager(this))
, m_reply(nullptr) , m_reply(nullptr)
, m_isManuallyRequested(false)
{ {
} }