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 e37509e00ad768d971d28ba8c05ee0f551922528
Author: yushuailong <[email protected]>
AuthorDate: Fri Sep 11 20:15:32 2026 +0800

    sched/clock: Fix timekeeping adjtime convergence.
    
    Clamp each wall-clock adjustment in both positive and negative directions, 
then subtract the applied amount from the remaining adjustment. This makes 
adjtime converge to zero and prevents large negative adjustments from slewing 
the clock in the wrong direction.
    
    Assisted-by: OpenAI Codex
    Signed-off-by: yushuailong <[email protected]>
---
 sched/clock/clock_timekeeping.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)

diff --git a/sched/clock/clock_timekeeping.c b/sched/clock/clock_timekeeping.c
index 632cebc989e..4d5a48b684c 100644
--- a/sched/clock/clock_timekeeping.c
+++ b/sched/clock/clock_timekeeping.c
@@ -247,13 +247,22 @@ void clock_update_wall_time(void)
 
   if (g_clock_adjust != 0 && sec > 0)
     {
-      long adjust = NTP_MAX_ADJUST * (long)sec;
-      if (g_clock_adjust < adjust && g_clock_adjust > -adjust)
+      long limit = NTP_MAX_ADJUST * (long)sec;
+      long adjust = g_clock_adjust;
+
+      /* Limit the adjustment while preserving its direction. */
+
+      if (adjust > limit)
+        {
+          adjust = limit;
+        }
+      else if (adjust < -limit)
         {
-          adjust = g_clock_adjust;
+          adjust = -limit;
         }
 
       nsec += adjust * NSEC_PER_USEC;
+      g_clock_adjust -= adjust;
 
       while (nsec < 0)
         {

Reply via email to