From 6b6fda85bafa91f3822fb86900e7c31b017686e6 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Anian=20B=C3=BChler?=
 <anian.buehler@reutlingen-university.de>
Date: Tue, 4 Jun 2024 11:29:12 +0200
Subject: [PATCH] Fixed buffer overflow by unreadable data

---
 src/DidacticNet.cpp | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)

diff --git a/src/DidacticNet.cpp b/src/DidacticNet.cpp
index 52e0fc9..4aeaf5e 100644
--- a/src/DidacticNet.cpp
+++ b/src/DidacticNet.cpp
@@ -130,9 +130,7 @@ bool DidacticPSNet::recieveData()
   static int msgCounter = 0;
   static int topicCounter = 0;
   static int payloadCounter = 0;
-  // if(msgCounter == NULL){	msgCounter = 0;	}
-  // if(topicCounter == NULL){ topicCounter = 0; }
-  // if(dataCounter == NULL){ dataCounter = 0; }
+
   while (checkData())
   {
     char localBuffer = _port->read();
@@ -159,8 +157,16 @@ bool DidacticPSNet::recieveData()
     }
     else if (_readBufferMessage[0] == MSG_PRELIMITER && localBuffer != MSG_DELIMITER)
     {
-      msgCounter++;
-      _readBufferMessage[msgCounter] = localBuffer;
+      if (msgCounter > LEN_OVERHEAD + MAX_LEN_TOPICS + MAX_LEN_PAYLOAD)
+      {
+        msgCounter == 0;
+        _readBufferMessage[0] = '\0';
+      }
+      else
+      {
+        msgCounter++;
+        _readBufferMessage[msgCounter] = localBuffer;
+      }
     }
   }
   return false;
-- 
GitLab