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 18ec054e961c5b1124f4d458aad0be663ad7ef5d
Author: ligd <[email protected]>
AuthorDate: Fri Feb 13 12:17:21 2026 +0800

    libc/time: Avoid the recursive zone loading in gmtsub().
    
    gmtsub() loads the UTC zone the first time it runs.  The load may log
    through syslog, for example when the zone file does not exist, and a
    syslog timestamp calls gmtime_r() on the same thread.  g_gmt_lock is a
    recursive mutex, so the nested call took it again, found the zone still
    not loaded and started another load, recursing until the stack
    overflowed.
    
    Skip the load when the calling thread already holds g_gmt_lock, as
    tzset() does for g_lcl_lock, and convert as plain UTC until the zone is
    loaded.  This also keeps the nested call away from the half-loaded
    state.
    
    Signed-off-by: ligd <[email protected]>
    Signed-off-by: ganjing <[email protected]>
---
 libs/libc/time/lib_localtime.c | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)

diff --git a/libs/libc/time/lib_localtime.c b/libs/libc/time/lib_localtime.c
index 541943b9732..e1278c4f12d 100644
--- a/libs/libc/time/lib_localtime.c
+++ b/libs/libc/time/lib_localtime.c
@@ -2119,7 +2119,14 @@ static FAR struct tm *localsub(FAR const time_t *timep,
 static FAR struct tm *gmtsub(FAR const time_t *timep,
                              int_fast32_t offset, FAR struct tm *tmp)
 {
-  if (!g_gmt_isset)
+  FAR const struct state_s *sp = NULL;
+
+  /* gmtload() may log through syslog, which formats its timestamp with
+   * gmtime_r() and comes back here on the same thread.  Do not load again
+   * in that case, convert as plain UTC instead.
+   */
+
+  if (!g_gmt_isset && !nxrmutex_is_hold(&g_gmt_lock))
     {
       tz_lock(&g_gmt_lock);
 
@@ -2136,9 +2143,14 @@ static FAR struct tm *gmtsub(FAR const time_t *timep,
       tz_unlock(&g_gmt_lock);
     }
 
+  if (g_gmt_isset)
+    {
+      sp = g_gmt_ptr;
+    }
+
   tmp->tm_zone = ((FAR char *)(offset ? g_wildabbr :
-                               g_gmt_ptr ? g_gmt_ptr->chars : g_utc));
-  return timesub(timep, offset, g_gmt_ptr, tmp);
+                               sp ? sp->chars : g_utc));
+  return timesub(timep, offset, sp, tmp);
 }
 
 /* Return the number of leap years through the end of the given year

Reply via email to