Ver Fonte

Reject malformed numeric PreValue values safely

SybexXx há 2 dias atrás
pai
commit
fdd1fb6947

+ 18 - 2
code/components/jomjol_flowcontroll/ClassFlowPostProcessing.cpp

@@ -6,6 +6,8 @@
 #include <sstream>
 #include <new>
 #include <memory>
+#include <cerrno>
+#include <cstdlib>
 
 #include <time.h>
 
@@ -189,6 +191,13 @@ bool ClassFlowPostProcessing::LoadPreValue(void) {
                 LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Invalid PreValue entry for '" + name + "'");
                 return false;
             }
+            char* valueEnd = NULL;
+            errno = 0;
+            const double parsedValue = strtod(zwvalue.c_str(), &valueEnd);
+            if (valueEnd == zwvalue.c_str() || *valueEnd != '\0' || errno == ERANGE) {
+                LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Invalid numeric PreValue for '" + name + "'");
+                return false;
+            }
 
             int yy = 0, month = 0, dd = 0, hh = 0, mm = 0, ss = 0;
             struct tm whenStart = {};
@@ -212,7 +221,7 @@ bool ClassFlowPostProcessing::LoadPreValue(void) {
 
             for (int j = 0; j < NUMBERS.size(); ++j) {
                 if (NUMBERS[j]->name == name) {
-                    NUMBERS[j]->PreValue = stod(zwvalue.c_str());
+                    NUMBERS[j]->PreValue = parsedValue;
                     NUMBERS[j]->ReturnPreValue = RundeOutput(NUMBERS[j]->PreValue, NUMBERS[j]->Nachkomma + 1);
                     NUMBERS[j]->timeStampLastPreValue = parsedTime;
 
@@ -251,6 +260,13 @@ bool ClassFlowPostProcessing::LoadPreValue(void) {
             LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Invalid legacy PreValue number");
             return false;
         }
+        char* valueEnd = NULL;
+        errno = 0;
+        const double parsedValue = strtod(zwvalue.c_str(), &valueEnd);
+        if (valueEnd == zwvalue.c_str() || *valueEnd != '\0' || errno == ERANGE) {
+            LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Invalid numeric legacy PreValue");
+            return false;
+        }
 
         int yy = 0, month = 0, dd = 0, hh = 0, mm = 0, ss = 0;
         struct tm whenStart = {};
@@ -280,7 +296,7 @@ bool ClassFlowPostProcessing::LoadPreValue(void) {
             return false;
         }
 
-        NUMBERS[0]->PreValue = stod(zwvalue.c_str());
+        NUMBERS[0]->PreValue = parsedValue;
         NUMBERS[0]->timeStampLastPreValue = parsedTime;
         NUMBERS[0]->Value = NUMBERS[0]->PreValue;
         NUMBERS[0]->ReturnValue = to_string(NUMBERS[0]->Value);