These functions are POSIX.1-2001. We should have them in all environments we care about. Use them as they are thread-safe. Signed-off-by: Thomas Haller <thaller@xxxxxxxxxx> --- src/meta.c | 41 ++++++++++++++++++++++------------------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/src/meta.c b/src/meta.c index 0d4ae0261ff2..d087e56e1b84 100644 --- a/src/meta.c +++ b/src/meta.c @@ -387,7 +387,7 @@ static void date_type_print(const struct expr *expr, struct output_ctx *octx) { uint64_t tstamp64 = mpz_get_uint64(expr->value); time_t tstamp; - struct tm *tm, *cur_tm; + struct tm tm; char timestr[21]; /* Convert from nanoseconds to seconds */ @@ -395,14 +395,12 @@ static void date_type_print(const struct expr *expr, struct output_ctx *octx) /* Obtain current tm, to add tm_gmtoff to the timestamp */ tstamp = tstamp64; - cur_tm = localtime(&tstamp); - - if (cur_tm) - tstamp64 += cur_tm->tm_gmtoff; + if (localtime_r(&tstamp, &tm)) + tstamp64 += tm.tm_gmtoff; tstamp = tstamp64; - if ((tm = gmtime(&tstamp)) != NULL && - strftime(timestr, sizeof(timestr) - 1, "%Y-%m-%d %T", tm)) + if (gmtime_r(&tstamp, &tm) && + strftime(timestr, sizeof(timestr) - 1, "%Y-%m-%d %T", &tm)) nft_print(octx, "\"%s\"", timestr); else nft_print(octx, "Error converting timestamp to printed time"); @@ -410,7 +408,8 @@ static void date_type_print(const struct expr *expr, struct output_ctx *octx) static bool parse_iso_date(uint64_t *tstamp, const char *sym) { - struct tm tm, *cur_tm; + struct tm cur_tm; + struct tm tm; time_t ts; memset(&tm, 0, sizeof(struct tm)); @@ -432,14 +431,15 @@ success: */ ts = timegm(&tm); - /* Obtain current tm as well (at the specified time), so that we can substract tm_gmtoff */ - cur_tm = localtime(&ts); + if (ts == (time_t) -1) + return false; - if (ts == (time_t) -1 || cur_tm == NULL) + /* Obtain current tm as well (at the specified time), so that we can substract tm_gmtoff */ + if (!localtime_r(&ts, &cur_tm)) return false; /* Substract tm_gmtoff to get the current time */ - *tstamp = ts - cur_tm->tm_gmtoff; + *tstamp = ts - cur_tm.tm_gmtoff; return true; } @@ -494,15 +494,13 @@ static void day_type_print(const struct expr *expr, struct output_ctx *octx) static void hour_type_print(const struct expr *expr, struct output_ctx *octx) { uint32_t seconds = mpz_get_uint32(expr->value), minutes, hours; - struct tm *cur_tm; + struct tm cur_tm; time_t ts; /* Obtain current tm, so that we can add tm_gmtoff */ ts = time(NULL); - cur_tm = localtime(&ts); - - if (cur_tm) - seconds = (seconds + cur_tm->tm_gmtoff) % SECONDS_PER_DAY; + if (ts != ((time_t) -1) && localtime_r(&ts, &cur_tm)) + seconds = (seconds + cur_tm.tm_gmtoff) % SECONDS_PER_DAY; minutes = seconds / 60; seconds %= 60; @@ -520,7 +518,9 @@ static struct error_record *hour_type_parse(struct parse_ctx *ctx, struct expr **res) { struct error_record *er; - struct tm tm, *cur_tm; + struct tm cur_tm_data; + struct tm *cur_tm; + struct tm tm; uint32_t result; uint64_t tmp; char *endptr; @@ -537,7 +537,10 @@ static struct error_record *hour_type_parse(struct parse_ctx *ctx, /* Obtain current tm, so that we can substract tm_gmtoff */ ts = time(NULL); - cur_tm = localtime(&ts); + if (ts != ((time_t) -1) && localtime_r(&ts, &cur_tm_data)) + cur_tm = &cur_tm_data; + else + cur_tm = NULL; endptr = strptime(sym->identifier, "%T", &tm); if (endptr && *endptr == '\0') -- 2.41.0