netfilter-devel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Thomas Haller <thaller@redhat.com>
To: NetFilter <netfilter-devel@vger.kernel.org>
Cc: Thomas Haller <thaller@redhat.com>
Subject: [nft PATCH 2/2] meta: use reentrant localtime_r()/gmtime_r() functions
Date: Tue, 22 Aug 2023 10:13:10 +0200	[thread overview]
Message-ID: <20230822081318.1370371-2-thaller@redhat.com> (raw)
In-Reply-To: <20230822081318.1370371-1-thaller@redhat.com>

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>
---
 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


  reply	other threads:[~2023-08-22  8:17 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-22  8:13 [nft PATCH 1/2] meta: don't assume time_t is 64 bit in date_type_print() Thomas Haller
2023-08-22  8:13 ` Thomas Haller [this message]
2023-08-22  8:54   ` [nft PATCH 2/2] meta: use reentrant localtime_r()/gmtime_r() functions Pablo Neira Ayuso
2023-08-22 11:39     ` Thomas Haller
2023-08-22 15:15       ` Thomas Haller
2023-08-22 16:06         ` Pablo Neira Ayuso
2023-08-22 16:04       ` Pablo Neira Ayuso
2023-08-22  8:54 ` [nft PATCH 1/2] meta: don't assume time_t is 64 bit in date_type_print() Pablo Neira Ayuso
2023-08-22  9:06 ` Pablo Neira Ayuso

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230822081318.1370371-2-thaller@redhat.com \
    --to=thaller@redhat.com \
    --cc=netfilter-devel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).