You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by da...@apache.org on 2021/07/02 16:27:33 UTC

[incubator-nuttx] 05/09: libc/time: Avoid modify the global variables concurrently in tzset

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

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

commit 26908f472e57a50b991f00943d872792ab28eb8d
Author: Xiang Xiao <xi...@xiaomi.com>
AuthorDate: Mon Jun 21 02:21:28 2021 +0800

    libc/time: Avoid modify the global variables concurrently in tzset
    
    Signed-off-by: Xiang Xiao <xi...@xiaomi.com>
    Change-Id: I7fb6439c3a669ef07c1756ccc294cd487805a510
---
 libs/libc/time/lib_localtime.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)

diff --git a/libs/libc/time/lib_localtime.c b/libs/libc/time/lib_localtime.c
index e9f2f81..647ce51 100644
--- a/libs/libc/time/lib_localtime.c
+++ b/libs/libc/time/lib_localtime.c
@@ -1664,11 +1664,8 @@ static void gmtload(FAR struct state_s *const sp)
 
 static void tzsetwall(void)
 {
-  tz_semtake(&g_lcl_sem);
-
   if (g_lcl_isset < 0)
     {
-      tz_semgive(&g_lcl_sem);
       return;
     }
 
@@ -1685,8 +1682,6 @@ static void tzsetwall(void)
   settzname();
 
   g_lcl_isset = -1;
-
-  tz_semgive(&g_lcl_sem);
 }
 
 /* The easy way to behave "as if no library function calls" localtime
@@ -2523,16 +2518,18 @@ void tzset(void)
 {
   FAR const char *name;
 
+  tz_semtake(&g_lcl_sem);
+
   name = getenv("TZ");
   if (name == NULL)
     {
       tzsetwall();
-      return;
+      goto out;
     }
 
   if (g_lcl_isset > 0 && strcmp(g_lcl_tzname, name) == 0)
     {
-      return;
+      goto out;
     }
 
   g_lcl_isset = strlen(name) < sizeof g_lcl_tzname;
@@ -2547,7 +2544,7 @@ void tzset(void)
       if (lclptr == NULL)
         {
           settzname(); /* all we can do */
-          return;
+          goto out;
         }
     }
 
@@ -2572,6 +2569,9 @@ void tzset(void)
     }
 
   settzname();
+
+out:
+  tz_semgive(&g_lcl_sem);
 }
 
 FAR struct tm *localtime(FAR const time_t * const timep)