From de1ab2cd57af884c6b2389b7a24640c1f24e61ec Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Tue, 18 Jun 2024 14:58:30 +0200 Subject: [PATCH] nstrftime: Fix crash on NetBSD 10.0. * lib/strftime.c (utc_timezone_cache): New variable. (utc_timezone): New function. (underlying_strftime): Use it when tz is NULL. --- ChangeLog | 7 +++++++ lib/strftime.c | 28 ++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/ChangeLog b/ChangeLog index cb44449076..0c39edd886 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2024-06-18 Bruno Haible + + nstrftime: Fix crash on NetBSD 10.0. + * lib/strftime.c (utc_timezone_cache): New variable. + (utc_timezone): New function. + (underlying_strftime): Use it when tz is NULL. + 2024-06-18 Bruno Haible c-nstrftime: Fix warning on platforms without strftime_l. diff --git a/lib/strftime.c b/lib/strftime.c index a757b4f313..ffc1670f23 100644 --- a/lib/strftime.c +++ b/lib/strftime.c @@ -403,6 +403,25 @@ c_locale (void) #endif +#if HAVE_NATIVE_TIME_Z + +/* Cache for the UTC time zone object. + Marked volatile so that different threads see the same value + (avoids locking). */ +static volatile timezone_t utc_timezone_cache; + +/* Return the UTC time zone object, or (timezone_t) 0 with errno set + if it cannot be created. */ +static timezone_t +utc_timezone (void) +{ + if (!utc_timezone_cache) + utc_timezone_cache = tzalloc ("UTC"); + return utc_timezone_cache; +} + +#endif + #if (defined __NetBSD__ || defined __sun) && REQUIRE_GNUISH_STRFTIME_AM_PM @@ -877,6 +896,15 @@ underlying_strftime (timezone_t tz, char *ubuf, size_t ubufsize, *u++ = format_char; *u = '\0'; +# if HAVE_NATIVE_TIME_Z + if (!tz) + { + tz = utc_timezone (); + if (!tz) + return 0; /* errno is set here */ + } +# endif + # if !HAVE_NATIVE_TIME_Z if (tz && tz != local_tz) { -- 2.39.5