From 26908f472e57a50b991f00943d872792ab28eb8d Mon Sep 17 00:00:00 2001 From: Xiang Xiao Date: Mon, 21 Jun 2021 02:21:28 +0800 Subject: [PATCH] libc/time: Avoid modify the global variables concurrently in tzset Signed-off-by: Xiang Xiao 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 e9f2f81236..647ce51542 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)