From 43cd776d7dc8ac573262f8459edeb1c1f5f3cd09 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Thu, 7 Mar 2024 16:18:32 -0500 Subject: xtime: Call tzset() from main() instead of lazily POSIX specifies[1] that If a thread accesses tzname, daylight, or timezone directly while another thread is in a call to tzset(), or to any function that is required or allowed to set timezone information as if by calling tzset(), the behavior is undefined. So calling it lazily from arbitrary threads is risky. [1]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/tzset.html --- src/xtime.c | 34 +--------------------------------- 1 file changed, 1 insertion(+), 33 deletions(-) (limited to 'src/xtime.c') diff --git a/src/xtime.c b/src/xtime.c index 4309289..8100f6c 100644 --- a/src/xtime.c +++ b/src/xtime.c @@ -11,38 +11,6 @@ #include #include -/** Call tzset() if necessary. */ -static void xtzset(void) { - static atomic bool is_set = false; - - if (!load(&is_set, relaxed)) { - tzset(); - store(&is_set, true, relaxed); - } -} - -int xlocaltime(const time_t *timep, struct tm *result) { - // Should be called before localtime_r() according to POSIX.1-2004 - xtzset(); - - if (localtime_r(timep, result)) { - return 0; - } else { - return -1; - } -} - -int xgmtime(const time_t *timep, struct tm *result) { - // Should be called before gmtime_r() according to POSIX.1-2004 - xtzset(); - - if (gmtime_r(timep, result)) { - return 0; - } else { - return -1; - } -} - int xmktime(struct tm *tm, time_t *timep) { *timep = mktime(tm); @@ -50,7 +18,7 @@ int xmktime(struct tm *tm, time_t *timep) { int error = errno; struct tm tmp; - if (xlocaltime(timep, &tmp) != 0) { + if (!localtime_r(timep, &tmp)) { return -1; } -- cgit v1.2.3