fix: Fix bootloop logging crash (#1357)

## Summary

* **What is the goal of this PR?** On a cold boot (or after a crash that
corrupts RTC RAM), logHead contains garbage. Then addToLogRingBuffer
does: ``strncpy(logMessages[logHead], message, MAX_ENTRY_LEN - 1); ``
With garbage logHead, this computes a completely invalid address. The %
MAX_LOG_LINES guard on line 16 only runs after the bad store, which is
too late. The fix is to clamp logHead before use.

## Additional Context

* Add any other information that might be helpful for the reviewer
(e.g., performance implications, potential risks,
  specific areas to focus on).

---

### AI Usage

While CrossPoint doesn't have restrictions on AI tools in contributing,
please be transparent about their usage as it
helps set the right context for reviewers.

Did you use AI tools to help write this code? _**NO**_ (did use claude
for the magic hash value)
This commit is contained in:
jpirnay
2026-03-09 21:53:38 +01:00
committed by GitHub
parent e60ba7620d
commit 4104fa8102
3 changed files with 48 additions and 2 deletions

View File

@@ -76,6 +76,14 @@ void begin() {
// `clearPanic()` to clear it after dumping
if (!isRebootFromPanic()) {
clearPanic();
} 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).
// 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();
}
}
}