summaryrefslogtreecommitdiffstats
path: root/src/meta.c
diff options
context:
space:
mode:
authorThomas Haller <thaller@redhat.com>2023-08-22 10:57:37 +0200
committerPablo Neira Ayuso <pablo@netfilter.org>2023-08-22 11:04:49 +0200
commit7b671ff19a43a20755a4ee34ed39bd4d10267b56 (patch)
tree56bcf9d24aeaa909a94b3e51ae86c5c2eb6da000 /src/meta.c
parentb8c54499cc4bac3fd86f32eefae0ce67e83fb5b8 (diff)
meta: use reentrant localtime_r()/gmtime_r() functions
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@redhat.com> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
Diffstat (limited to 'src/meta.c')
-rw-r--r--src/meta.c41
1 files changed, 22 insertions, 19 deletions
diff --git a/src/meta.c b/src/meta.c
index aa538caf..bf220100 100644
--- a/src/meta.c
+++ b/src/meta.c
@@ -386,23 +386,21 @@ const struct datatype ifname_type = {
static void date_type_print(const struct expr *expr, struct output_ctx *octx)
{
uint64_t tstamp64 = mpz_get_uint64(expr->value);
- struct tm *tm, *cur_tm;
char timestr[21];
time_t tstamp;
+ struct tm tm;
/* Convert from nanoseconds to seconds */
tstamp64 /= 1000000000L;
/* 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,10 +518,12 @@ 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;
uint32_t result;
uint64_t tmp;
char *endptr;
+ struct tm tm;
time_t ts;
memset(&tm, 0, sizeof(struct tm));
@@ -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')