From abe0a34fc0092937364c9045d7514a8ef568af74 Mon Sep 17 00:00:00 2001 From: Tom Fifield Date: Fri, 18 Jul 2025 20:49:19 +1000 Subject: [PATCH 1/2] Add additional Epoch check for time set (#7375) We have two perhapsSetRTC functions, which are called to set the time. The one with 3 parameters had a helpful check to reject an invalid time, by comparing the time from the source against when the firmware was compiled. The one with 2 parameters, which is called from the GPS lookForTime did not. As a result, certain GPS with bad time handling could set a time that was in the past. This patch adds the same epoch check code to the other perhapsSetRTC method. Fixes https://github.com/meshtastic/firmware/issues/7364 --- src/gps/RTC.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/gps/RTC.cpp b/src/gps/RTC.cpp index 5054be3f0..c4d6065ff 100644 --- a/src/gps/RTC.cpp +++ b/src/gps/RTC.cpp @@ -228,6 +228,13 @@ RTCSetResult perhapsSetRTC(RTCQuality q, struct tm &t) tv.tv_sec = res; tv.tv_usec = 0; // time.centisecond() * (10 / 1000); +#ifdef BUILD_EPOCH + if (tv->tv_sec < BUILD_EPOCH) { + LOG_WARN("Ignore time (%ld) before build epoch (%ld)!", printableEpoch, BUILD_EPOCH); + return RTCSetResultInvalidTime; + } +#endif + // LOG_DEBUG("Got time from GPS month=%d, year=%d, unixtime=%ld", t.tm_mon, t.tm_year, tv.tv_sec); if (t.tm_year < 0 || t.tm_year >= 300) { // LOG_DEBUG("Ignore invalid GPS month=%d, year=%d, unixtime=%ld", t.tm_mon, t.tm_year, tv.tv_sec); From cf574c71d8dbf6a28e279286b90deb7658e10bb4 Mon Sep 17 00:00:00 2001 From: Ben Meadors Date: Fri, 18 Jul 2025 09:24:34 -0500 Subject: [PATCH 2/2] Fix build --- src/gps/RTC.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/gps/RTC.cpp b/src/gps/RTC.cpp index c4d6065ff..d574c9ad0 100644 --- a/src/gps/RTC.cpp +++ b/src/gps/RTC.cpp @@ -226,10 +226,10 @@ RTCSetResult perhapsSetRTC(RTCQuality q, struct tm &t) time_t res = gm_mktime(&t); struct timeval tv; tv.tv_sec = res; - tv.tv_usec = 0; // time.centisecond() * (10 / 1000); - + tv.tv_usec = 0; // time.centisecond() * (10 / 1000); + uint32_t printableEpoch = tv.tv_sec; // Print lib only supports 32 bit but time_t can be 64 bit on some platforms #ifdef BUILD_EPOCH - if (tv->tv_sec < BUILD_EPOCH) { + if (tv.tv_sec < BUILD_EPOCH) { LOG_WARN("Ignore time (%ld) before build epoch (%ld)!", printableEpoch, BUILD_EPOCH); return RTCSetResultInvalidTime; }