From 3a92e4aab9efd9b3dcc5f0c3b752ca0ba16c5484 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sami=20V=C3=A4nttinen?= Date: Tue, 3 Apr 2018 05:13:07 +0300 Subject: [PATCH] Socket buffer size fix (#1720) --- src/browser/BrowserAction.cpp | 5 +++-- src/browser/BrowserService.h | 2 -- src/browser/NativeMessagingBase.h | 7 +++++++ src/browser/NativeMessagingHost.cpp | 8 +++++++- src/proxy/NativeMessagingHost.cpp | 7 +++++++ 5 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/browser/BrowserAction.cpp b/src/browser/BrowserAction.cpp index b15d8ed59..7519c0e81 100755 --- a/src/browser/BrowserAction.cpp +++ b/src/browser/BrowserAction.cpp @@ -20,6 +20,7 @@ #include #include "BrowserAction.h" #include "BrowserSettings.h" +#include "NativeMessagingBase.h" #include "sodium.h" #include "sodium/crypto_box.h" #include "sodium/randombytes.h" @@ -456,7 +457,7 @@ QString BrowserAction::encrypt(const QString plaintext, const QString nonce) std::vector sk(sa.cbegin(), sa.cend()); std::vector e; - e.resize(max_length); + e.resize(NATIVE_MSG_MAX_LENGTH); if (m.empty() || n.empty() || ck.empty() || sk.empty()) { return QString(); @@ -484,7 +485,7 @@ QByteArray BrowserAction::decrypt(const QString encrypted, const QString nonce) std::vector sk(sa.cbegin(), sa.cend()); std::vector d; - d.resize(max_length); + d.resize(NATIVE_MSG_MAX_LENGTH); if (m.empty() || n.empty() || ck.empty() || sk.empty()) { return QByteArray(); diff --git a/src/browser/BrowserService.h b/src/browser/BrowserService.h index 5a96e1ecd..08007d831 100644 --- a/src/browser/BrowserService.h +++ b/src/browser/BrowserService.h @@ -25,8 +25,6 @@ #include "gui/DatabaseTabWidget.h" #include "core/Entry.h" -enum { max_length = 16*1024 }; - class BrowserService : public QObject { Q_OBJECT diff --git a/src/browser/NativeMessagingBase.h b/src/browser/NativeMessagingBase.h index 4253b7585..26e0a9902 100644 --- a/src/browser/NativeMessagingBase.h +++ b/src/browser/NativeMessagingBase.h @@ -32,6 +32,13 @@ #include #include +#ifndef Q_OS_WIN +#include +#include +#endif + +static const int NATIVE_MSG_MAX_LENGTH = 1024*1024; + class NativeMessagingBase : public QObject { Q_OBJECT diff --git a/src/browser/NativeMessagingHost.cpp b/src/browser/NativeMessagingHost.cpp index 0101e9444..88b7dd722 100755 --- a/src/browser/NativeMessagingHost.cpp +++ b/src/browser/NativeMessagingHost.cpp @@ -142,11 +142,17 @@ void NativeMessagingHost::newLocalConnection() void NativeMessagingHost::newLocalMessage() { QLocalSocket* socket = qobject_cast(QObject::sender()); - if (!socket || socket->bytesAvailable() <= 0) { return; } + socket->setReadBufferSize(NATIVE_MSG_MAX_LENGTH); + int socketDesc = socket->socketDescriptor(); + if (socketDesc) { + int max = NATIVE_MSG_MAX_LENGTH; + setsockopt(socketDesc, SOL_SOCKET, SO_SNDBUF, &max, sizeof(max)); + } + QByteArray arr = socket->readAll(); if (arr.isEmpty()) { return; diff --git a/src/proxy/NativeMessagingHost.cpp b/src/proxy/NativeMessagingHost.cpp index c5ce60ea9..15416dc8d 100755 --- a/src/proxy/NativeMessagingHost.cpp +++ b/src/proxy/NativeMessagingHost.cpp @@ -22,6 +22,13 @@ NativeMessagingHost::NativeMessagingHost() : NativeMessagingBase() { m_localSocket = new QLocalSocket(); m_localSocket->connectToServer(getLocalServerPath()); + m_localSocket->setReadBufferSize(NATIVE_MSG_MAX_LENGTH); + + int socketDesc = m_localSocket->socketDescriptor(); + if (socketDesc) { + int max = NATIVE_MSG_MAX_LENGTH; + setsockopt(socketDesc, SOL_SOCKET, SO_SNDBUF, &max, sizeof(max)); + } #ifdef Q_OS_WIN m_running.store(true); m_future = QtConcurrent::run(this, static_cast(&NativeMessagingHost::readNativeMessages));