Browse Source

buffer overflow fix for gui

Cyamond 5 years ago
parent
commit
02826240ad
1 changed files with 14 additions and 10 deletions
  1. 14 10
      gui/src/qmlhandler.cpp

+ 14 - 10
gui/src/qmlhandler.cpp

@@ -1,3 +1,4 @@
+#include <QGuiApplication>
 #include <csignal>
 #include <cstdio>
 #include <cstdlib>
@@ -7,7 +8,6 @@
 #include <sys/wait.h>
 #include <thread>
 #include <unistd.h>
-#include <QGuiApplication>
 
 #include "qmlhandler.h"
 #include <boost/asio.hpp>
@@ -32,10 +32,11 @@ void QMLHandler::onExit() {
 void QMLHandler::handleJSON(string buffer) {
   Json::Value root;
   Json::CharReaderBuilder builder;
-  Json::CharReader * reader = builder.newCharReader();
+  Json::CharReader *reader = builder.newCharReader();
   string jsonError;
 
-  bool parsingSuccessful = reader->parse(buffer.c_str(), buffer.c_str() + buffer.size(), &root, &jsonError);
+  bool parsingSuccessful = reader->parse(
+      buffer.c_str(), buffer.c_str() + buffer.size(), &root, &jsonError);
 
   if (!parsingSuccessful) {
     return;
@@ -63,17 +64,18 @@ void QMLHandler::readPipeLoop() {
     poll(&inPipeStatus, 1, 100);
 
     if (inPipeStatus.revents & POLLIN) {
-      readOffset += read(inpipefd[0], buf + readOffset, 1024);
+      readOffset += read(inpipefd[0], buf + readOffset, 1);
 
       pollCount = 0;
 
-      buf[1023] = 0;
-      buf[strlen(buf)] = 0;
     } else {
       pollCount++;
     }
 
     if (pollCount > 9 && buf[0]) {
+      buf[1023] = 0;
+      buf[strlen(buf)] = 0;
+
       string cleanBuffer = buf + strcspn(buf, "\n") + 1;
       string receivedData = cleanBuffer.substr(0, cleanBuffer.size() - 1);
 
@@ -99,13 +101,15 @@ void QMLHandler::onSendingSelectFileButton(QUrl url) {
 
 void QMLHandler::onSendingSendFileButton() {
   QString command = "put " + sendFileUrl.toString() + "\n";
-  write(outpipefd[1], command.toUtf8().constData(), strlen(command.toUtf8().constData()));
+  write(outpipefd[1], command.toUtf8().constData(),
+        strlen(command.toUtf8().constData()));
 }
 
 // Receiving
 void QMLHandler::onReceivingGetFileButton(QString fileName) {
-    QString command = "get " + fileName + "\n";
-    write(outpipefd[1], command.toUtf8().constData(), strlen(command.toUtf8().constData()));
+  QString command = "get " + fileName + "\n";
+  write(outpipefd[1], command.toUtf8().constData(),
+        strlen(command.toUtf8().constData()));
 }
 
 // Messages
@@ -127,7 +131,7 @@ void QMLHandler::onIpPopupEnterIp(QString ip) {
     // Child
     dup2(outpipefd[0], STDIN_FILENO);
     dup2(inpipefd[1], STDOUT_FILENO);
-    //dup2(inpipefd[1], STDERR_FILENO);
+    // dup2(inpipefd[1], STDERR_FILENO);
 
     // ask kernel to deliver SIGTERM in case the parent dies
     prctl(PR_SET_PDEATHSIG, SIGTERM);