On 2024-06-18 06:02, Bruno Haible wrote:
tz=NULL means universal time (a.k.a. GMT) in our strftime.h, but is undefined
behaviour for NetBSD's functions.

Thanks for the fix. I tweaked it via the attached. The "0" at the end of "UTC0" is better if the zoneinfo files are missing.
From 562ed134d6cfa9956a72ff0d1c2e7aa84474c7c0 Mon Sep 17 00:00:00 2001
From: Paul Eggert <egg...@cs.ucla.edu>
Date: Tue, 18 Jun 2024 08:21:44 -0700
Subject: [PATCH] nstrftime: tweak volatile access

* lib/strftime.c (utc_timezone): Avoid unnecessary access to volatile.
---
 ChangeLog      |  5 +++++
 lib/strftime.c | 14 ++++++++------
 2 files changed, 13 insertions(+), 6 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 0c39edd886..1d5c743711 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2024-06-18  Paul Eggert  <egg...@cs.ucla.edu>
+
+	nstrftime: tweak volatile access
+	* lib/strftime.c (utc_timezone): Avoid unnecessary access to volatile.
+
 2024-06-18  Bruno Haible  <br...@clisp.org>
 
 	nstrftime: Fix crash on NetBSD 10.0.
diff --git a/lib/strftime.c b/lib/strftime.c
index ffc1670f23..e21a356e80 100644
--- a/lib/strftime.c
+++ b/lib/strftime.c
@@ -405,9 +405,10 @@ c_locale (void)
 
 #if HAVE_NATIVE_TIME_Z
 
-/* Cache for the UTC time zone object.
-   Marked volatile so that different threads see the same value
-   (avoids locking).  */
+/* On NetBSD a null tz has undefined behavior, so use a non-null tz.
+   Cache the UTC time zone object in a volatile variable for improved
+   thread safety.  This is good enough in practice, although in theory
+   stdatomic.h should be used.  */
 static volatile timezone_t utc_timezone_cache;
 
 /* Return the UTC time zone object, or (timezone_t) 0 with errno set
@@ -415,9 +416,10 @@ static volatile timezone_t utc_timezone_cache;
 static timezone_t
 utc_timezone (void)
 {
-  if (!utc_timezone_cache)
-    utc_timezone_cache = tzalloc ("UTC");
-  return utc_timezone_cache;
+  timezone_t tz = utc_timezone_cache;
+  if (!tz)
+    utc_timezone_cache = tz = tzalloc ("UTC0");
+  return tz;
 }
 
 #endif
-- 
2.43.0

Reply via email to