]> git.ipfire.org Git - thirdparty/nftables.git/commitdiff
meta: use reentrant localtime_r()/gmtime_r() functions
authorThomas Haller <thaller@redhat.com>
Tue, 22 Aug 2023 08:57:37 +0000 (10:57 +0200)
committerPablo Neira Ayuso <pablo@netfilter.org>
Tue, 22 Aug 2023 09:04:49 +0000 (11:04 +0200)
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>
src/meta.c

index aa538caf1621b102f9f6c727f0fca40c7ab2ac24..bf2201009a8c5dad9f8aee2dc4c0febb7a190616 100644 (file)
@@ -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')