This is an automated email from the ASF dual-hosted git repository.

xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit c11029848e85e13fb186399d9495002c717cad04
Author: yushuailong <[email protected]>
AuthorDate: Wed Jun 10 19:21:15 2026 +0800

    arch/xtensa/espressif: Fix non-atomic clock read in esp_rtc_rdalarm.
    
    esp_rtc_rdalarm() computed tv_sec and tv_nsec from two separate
    evaluations of esp_hr_timer_time_us() + offset + deadline. The
    high-resolution timer advances between the two calls, so a read that
    straddles a second boundary yields an inconsistent timespec.
    
    Compute the microsecond value once into a local variable and derive
    both fields from that single snapshot.
    
    Signed-off-by: yushuailong <[email protected]>
---
 arch/xtensa/src/common/espressif/esp_rtc.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/arch/xtensa/src/common/espressif/esp_rtc.c 
b/arch/xtensa/src/common/espressif/esp_rtc.c
index 2d08b1d056e..bb4e200b54e 100644
--- a/arch/xtensa/src/common/espressif/esp_rtc.c
+++ b/arch/xtensa/src/common/espressif/esp_rtc.c
@@ -611,6 +611,7 @@ static int esp_rtc_rdalarm(struct rtc_lowerhalf_s *lower,
   struct timespec ts;
   struct alm_cbinfo_s *cbinfo;
   irqstate_t flags;
+  uint64_t time_us;
 
   DEBUGASSERT(lower != NULL);
   DEBUGASSERT(alarminfo != NULL);
@@ -624,10 +625,11 @@ static int esp_rtc_rdalarm(struct rtc_lowerhalf_s *lower,
 
   cbinfo = &priv->alarmcb[alarminfo->id];
 
-  ts.tv_sec = (esp_hr_timer_time_us() + g_rtc_save->offset +
-              cbinfo->deadline_us) / USEC_PER_SEC;
-  ts.tv_nsec = ((esp_hr_timer_time_us() + g_rtc_save->offset +
-              cbinfo->deadline_us) % USEC_PER_SEC) * NSEC_PER_USEC;
+  time_us = esp_hr_timer_time_us() + g_rtc_save->offset +
+            cbinfo->deadline_us;
+
+  ts.tv_sec = time_us / USEC_PER_SEC;
+  ts.tv_nsec = (time_us % USEC_PER_SEC) * NSEC_PER_USEC;
 
   localtime_r(&ts.tv_sec, (struct tm *)alarminfo->time);
 

Reply via email to