浏览代码

Bugfix GUI - readOffset out of bounds

The bug is not completely fixed. For the real fix we need the CLI to
send minimized JSON with a newline at the end of every message.
Jonas Pflanzer 5 年之前
父节点
当前提交
4b39cb1a7b
共有 1 个文件被更改,包括 18 次插入6 次删除
  1. 18 6
      gui/src/qmlhandler.cpp

+ 18 - 6
gui/src/qmlhandler.cpp

@@ -20,7 +20,7 @@ using namespace std;
 
 int inpipefd[2];
 int outpipefd[2];
-char buf[1024];
+char buf[1025];
 QUrl sendFileUrl = QUrl("");
 
 bool programActive = true;
@@ -68,18 +68,22 @@ void QMLHandler::readPipeLoop() {
     if (inPipeStatus.revents & POLLIN) {
       readOffset += read(inpipefd[0], buf + readOffset, 1);
 
+      // We need this once we receive minimized JSON from the CLI
+      /*if(buf[readOffset-1] == '\n') {
+        pollCount = 10;
+      } else {
+        pollCount = 0;
+      }*/
       pollCount = 0;
 
     } else {
       pollCount++;
     }
 
-    if (pollCount > 9 && buf[0]) {
-      buf[1023] = 0;
-      buf[strlen(buf)] = 0;
+    if (pollCount > 9 && readOffset > 0) {
+      buf[strnlen(buf, 1024)] = 0;
 
-      string cleanBuffer = buf + strcspn(buf, "\n") + 1;
-      string receivedData = cleanBuffer.substr(0, cleanBuffer.size() - 1);
+      string receivedData = buf;
 
       emit log(QString::fromStdString(receivedData));
       qInfo() << QString::fromStdString(receivedData);
@@ -88,6 +92,14 @@ void QMLHandler::readPipeLoop() {
       pollCount = 0;
       readOffset = 0;
     }
+
+    // Fixme
+    if (readOffset >= 1024) {
+      qInfo() << "Fixme: QMLHandler::readPipeLoop() readOffset too high!!!";
+      readOffset = 0;
+      pollCount = 0;
+      memset(buf, 0, 1025);
+    }
   }
 }