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 a0a48601c19af2d93b9ec79e433dd0bc0f3961b1
Author: wangzhi16 <[email protected]>
AuthorDate: Thu Apr 23 14:44:54 2026 +0800

    libc/time: Copy the TZ value in tzset().
    
    getenv() returns a pointer into the environment of the task, which is
    protected only during the call.  tzset() kept using the pointer for the
    comparison with the cached name, for zoneinit() and for the copy into
    g_lcl_tzname, so a concurrent setenv() or unsetenv() could free it and
    make tzset() read freed memory.
    
    Copy the value into a local buffer of MY_TZNAME_MAX + 1 bytes, the size
    of g_lcl_tzname, right after getenv().
    
    Signed-off-by: wangzhi16 <[email protected]>
    Signed-off-by: liaoao <[email protected]>
    Signed-off-by: ganjing <[email protected]>
---
 libs/libc/time/lib_localtime.c | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/libs/libc/time/lib_localtime.c b/libs/libc/time/lib_localtime.c
index 293ee72142d..541943b9732 100644
--- a/libs/libc/time/lib_localtime.c
+++ b/libs/libc/time/lib_localtime.c
@@ -2859,6 +2859,7 @@ static int zoneinit(FAR const char *name)
 
 void tzset(void)
 {
+  char buf[MY_TZNAME_MAX + 1];
   FAR const char *name;
   int lcl;
 
@@ -2878,6 +2879,13 @@ void tzset(void)
     }
   else
     {
+      /* getenv() returns a pointer into the environment, which another
+       * thread may free with setenv() or unsetenv().  Work on a copy.
+       */
+
+      strlcpy(buf, name, sizeof(buf));
+      name = buf;
+
       if (g_lcl_isset > 0 && strcmp(g_lcl_tzname, name) == 0)
         {
           return;

Reply via email to