Clear log if invalid

This commit is contained in:
jpirnay
2026-03-08 16:05:08 +01:00
parent c464bdede8
commit a3a50c8c40
3 changed files with 13 additions and 5 deletions
+3 -1
View File
@@ -80,10 +80,12 @@ std::string getLastLogs() {
return output; return output;
} }
void sanitizeLogHead() { bool sanitizeLogHead() {
if (logHead >= MAX_LOG_LINES) { if (logHead >= MAX_LOG_LINES) {
logHead = 0; logHead = 0;
return true;
} }
return false;
} }
void clearLastLogs() { void clearLastLogs() {
+5 -3
View File
@@ -57,9 +57,11 @@ void logPrintf(const char* level, const char* origin, const char* format, ...);
std::string getLastLogs(); std::string getLastLogs();
void clearLastLogs(); void clearLastLogs();
// Clamps logHead into range without wiping messages — safe to call on panic // Clamps logHead into range. Returns true if logHead was out of range (repaired),
// reboots where the panic occurred before begin() ever had a chance to run. // which also means logMessages is untrusted garbage. Callers should call
void sanitizeLogHead(); // clearLastLogs() when this returns true so getLastLogs() does not dump corrupt
// data into crash reports.
bool sanitizeLogHead();
class MySerialImpl : public Print { class MySerialImpl : public Print {
public: public:
+5 -1
View File
@@ -79,7 +79,11 @@ void begin() {
} else { } else {
// Panic reboot: preserve logs and panic info, but clamp logHead in case the // 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). // 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();
}
} }
} }