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 045677b4110a244d9fdfc9b602e0585eb0f4eb26
Author: liwenxiang1 <[email protected]>
AuthorDate: Tue Mar 10 21:16:37 2026 +0800

    drivers/syslog: fix nx_vsyslog clobbering errno
    
    nx_vsyslog calls libc functions that modify errno as a side effect:
    - clock_gettime: use nxclock_gettime instead
    - strftime -> tzset -> open: sets ENOENT when timezone file not found
    Save and restore errno around the entire nx_vsyslog call to prevent
    syslog from clobbering the caller's errno.
    
    Signed-off-by: liwenxiang1 <[email protected]>
---
 drivers/syslog/vsyslog.c | 20 ++++++++++++++++++--
 1 file changed, 18 insertions(+), 2 deletions(-)

diff --git a/drivers/syslog/vsyslog.c b/drivers/syslog/vsyslog.c
index 5c8bbc831f3..b6464d801d1 100644
--- a/drivers/syslog/vsyslog.c
+++ b/drivers/syslog/vsyslog.c
@@ -27,6 +27,7 @@
 #include <nuttx/config.h>
 
 #include <stdio.h>
+#include <string.h>
 #include <syslog.h>
 #include <errno.h>
 
@@ -103,6 +104,13 @@ int nx_vsyslog(int priority, FAR const IPTR char *fmt, FAR 
va_list *ap)
 #  endif
 #endif
 
+  /* Save errno so that nx_vsyslog does not clobber it. The syslog
+   * formatting path calls libc functions (e.g. strftime -> tzset -> open)
+   * that may modify errno as a side effect.
+   */
+
+  int saved_errno = get_errno();
+
   /* Wrap the low-level output in a stream object and let lib_vsprintf
    * do the work.
    */
@@ -133,13 +141,13 @@ int nx_vsyslog(int priority, FAR const IPTR char *fmt, 
FAR va_list *ap)
 #  if defined(CONFIG_SYSLOG_TIMESTAMP_REALTIME)
       /* Use CLOCK_REALTIME if so configured */
 
-      clock_gettime(CLOCK_REALTIME, &ts);
+      nxclock_gettime(CLOCK_REALTIME, &ts);
 #  else
       /* Prefer monotonic when enabled, as it can be synchronized to
        * RTC with clock_resynchronize.
        */
 
-      clock_gettime(CLOCK_MONOTONIC, &ts);
+      nxclock_gettime(CLOCK_MONOTONIC, &ts);
 #  endif
 
       /* Prepend the message with the current time, if available */
@@ -311,5 +319,13 @@ int nx_vsyslog(int priority, FAR const IPTR char *fmt, FAR 
va_list *ap)
   /* Flush and destroy the syslog stream buffer */
 
   lib_syslograwstream_close(&stream);
+
+  /* Restore errno that may have been clobbered by libc calls
+   * (e.g. strftime -> tzset -> open sets ENOENT when timezone
+   * file is not found).
+   */
+
+  set_errno(saved_errno);
+
   return ret;
 }

Reply via email to