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/eval.c | 2 +- src/main.c | 4 ++++ src/parse.c | 12 ++++++------ src/printf.c | 4 ++-- src/xtime.c | 34 +--------------------------------- src/xtime.h | 24 ------------------------ 6 files changed, 14 insertions(+), 66 deletions(-) (limited to 'src') diff --git a/src/eval.c b/src/eval.c index 8b71833..1711001 100644 --- a/src/eval.c +++ b/src/eval.c @@ -730,7 +730,7 @@ bool eval_fls(const struct bfs_expr *expr, struct bfs_eval *state) { time_t six_months_ago = now - 6 * 30 * 24 * 60 * 60; time_t tomorrow = now + 24 * 60 * 60; struct tm tm; - if (xlocaltime(&time, &tm) != 0) { + if (!localtime_r(&time, &tm)) { goto error; } char time_str[256]; diff --git a/src/main.c b/src/main.c index 1ddbc54..16a2576 100644 --- a/src/main.c +++ b/src/main.c @@ -55,6 +55,7 @@ #include #include #include +#include #include /** @@ -121,6 +122,9 @@ int main(int argc, char *argv[]) { locale_err = errno; } + // Apply the environment's timezone + tzset(); + // Parse the command line struct bfs_ctx *ctx = bfs_parse_cmdline(argc, argv); if (!ctx) { diff --git a/src/parse.c b/src/parse.c index 5d0f333..b26a50f 100644 --- a/src/parse.c +++ b/src/parse.c @@ -1086,8 +1086,8 @@ static struct bfs_expr *parse_const(struct bfs_parser *parser, int value, int ar */ static struct bfs_expr *parse_daystart(struct bfs_parser *parser, int arg1, int arg2) { struct tm tm; - if (xlocaltime(&parser->now.tv_sec, &tm) != 0) { - parse_perror(parser, "xlocaltime()"); + if (!localtime_r(&parser->now.tv_sec, &tm)) { + parse_perror(parser, "localtime_r()"); return NULL; } @@ -1708,8 +1708,8 @@ static int parse_reftime(const struct bfs_parser *parser, struct bfs_expr *expr) fprintf(stderr, "Supported timestamp formats are ISO 8601-like, e.g.\n\n"); struct tm tm; - if (xlocaltime(&parser->now.tv_sec, &tm) != 0) { - parse_perror(parser, "xlocaltime()"); + if (!localtime_r(&parser->now.tv_sec, &tm)) { + parse_perror(parser, "localtime_r()"); return -1; } @@ -1728,8 +1728,8 @@ static int parse_reftime(const struct bfs_parser *parser, struct bfs_expr *expr) fprintf(stderr, " - %04d-%02d-%02dT%02d:%02d:%02d%+03d:%02d\n", year, month, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec, tz_hour, tz_min); - if (xgmtime(&parser->now.tv_sec, &tm) != 0) { - parse_perror(parser, "xgmtime()"); + if (!gmtime_r(&parser->now.tv_sec, &tm)) { + parse_perror(parser, "gmtime_r()"); return -1; } diff --git a/src/printf.c b/src/printf.c index 1fe8cc3..34ed606 100644 --- a/src/printf.c +++ b/src/printf.c @@ -123,7 +123,7 @@ static int bfs_printf_ctime(CFILE *cfile, const struct bfs_fmt *fmt, const struc } struct tm tm; - if (xlocaltime(&ts->tv_sec, &tm) != 0) { + if (!localtime_r(&ts->tv_sec, &tm)) { return -1; } @@ -153,7 +153,7 @@ static int bfs_printf_strftime(CFILE *cfile, const struct bfs_fmt *fmt, const st } struct tm tm; - if (xlocaltime(&ts->tv_sec, &tm) != 0) { + if (!localtime_r(&ts->tv_sec, &tm)) { return -1; } 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; } diff --git a/src/xtime.h b/src/xtime.h index 75d1f4e..fb60ae4 100644 --- a/src/xtime.h +++ b/src/xtime.h @@ -10,30 +10,6 @@ #include -/** - * localtime_r() wrapper that calls tzset() first. - * - * @param[in] timep - * The time_t to convert. - * @param[out] result - * Buffer to hold the result. - * @return - * 0 on success, -1 on failure. - */ -int xlocaltime(const time_t *timep, struct tm *result); - -/** - * gmtime_r() wrapper that calls tzset() first. - * - * @param[in] timep - * The time_t to convert. - * @param[out] result - * Buffer to hold the result. - * @return - * 0 on success, -1 on failure. - */ -int xgmtime(const time_t *timep, struct tm *result); - /** * mktime() wrapper that reports errors more reliably. * -- cgit v1.2.3