diff --git a/lib/Logging/Logging.cpp b/lib/Logging/Logging.cpp index a050ffe4..69a75cb6 100644 --- a/lib/Logging/Logging.cpp +++ b/lib/Logging/Logging.cpp @@ -80,10 +80,12 @@ std::string getLastLogs() { return output; } -void sanitizeLogHead() { +bool sanitizeLogHead() { if (logHead >= MAX_LOG_LINES) { logHead = 0; + return true; } + return false; } void clearLastLogs() { diff --git a/lib/Logging/Logging.h b/lib/Logging/Logging.h index fc2d9d37..2638c86e 100644 --- a/lib/Logging/Logging.h +++ b/lib/Logging/Logging.h @@ -57,9 +57,11 @@ void logPrintf(const char* level, const char* origin, const char* format, ...); std::string getLastLogs(); void clearLastLogs(); -// Clamps logHead into range without wiping messages — safe to call on panic -// reboots where the panic occurred before begin() ever had a chance to run. -void sanitizeLogHead(); +// Clamps logHead into range. Returns true if logHead was out of range (repaired), +// which also means logMessages is untrusted garbage. Callers should call +// clearLastLogs() when this returns true so getLastLogs() does not dump corrupt +// data into crash reports. +bool sanitizeLogHead(); class MySerialImpl : public Print { public: diff --git a/lib/hal/HalSystem.cpp b/lib/hal/HalSystem.cpp index dfe8055d..b92c95a1 100644 --- a/lib/hal/HalSystem.cpp +++ b/lib/hal/HalSystem.cpp @@ -79,7 +79,11 @@ void begin() { } else { // Panic reboot: preserve logs and panic info, but clamp logHead in case the // panic occurred before begin() ever ran (e.g. in a static constructor). - sanitizeLogHead(); + // If logHead was out of range, logMessages is also garbage — clear it so + // getLastLogs() does not dump corrupt data into the crash report. + if (sanitizeLogHead()) { + clearLastLogs(); + } } }