]> git.ipfire.org Git - thirdparty/ulogd2.git/commitdiff
output: JSON: fix output of GMT offset
authorJeremy Sowden <jeremy@azazel.net>
Tue, 30 Nov 2021 10:55:55 +0000 (10:55 +0000)
committerPablo Neira Ayuso <pablo@netfilter.org>
Mon, 3 Jan 2022 16:42:43 +0000 (17:42 +0100)
The compiler has two sets of complaints.  Firstly, `t->tm_gmtoffset` is
a `long int`, but it is being passed to `abs`, which leads to warnings
such as:

  ulogd_output_JSON.c:308:34: warning: absolute value function `abs` given an argument of type `long int` but has parameter of type `int` which may cause truncation of value

Secondly, it can't verify that the hour value derived from the offset
will in fact fit into `%02d`, thus:

  ulogd_output_JSON.c:306:37: warning: `%02d` directive output may be truncated writing between 2 and 6 bytes into a region of size 5

To remedy these, we now mod the offset by 86,400 and assign it to an `int`
before deriving the hour and minute values.

We also change the format-specifier for the hour value to `%+03d` which
causes a sign to be printed even if the value is positive, thus allowing
us not to specify the sign explicitly and to drop the `abs` call for the
hour value.

Signed-off-by: Jeremy Sowden <jeremy@azazel.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
output/ulogd_output_JSON.c

index 6edfa902efafd3ab806b440b1aace6c47fb51d40..8234e1b954cd5de596f6864083a5945301fc5b65 100644 (file)
@@ -302,11 +302,12 @@ static int json_interp(struct ulogd_pluginstance *upi)
                        now = time(NULL);
                t = localtime_r(&now, &result);
                if (unlikely(*opi->cached_tz = '\0' || t->tm_gmtoff != opi->cached_gmtoff)) {
+                       int gmtoff = t->tm_gmtoff % 86400;
+                       int gmtoff_hours = gmtoff / 3600;
+                       int gmtoff_minutes = abs(gmtoff) / 60 % 60;
+
                        snprintf(opi->cached_tz, sizeof(opi->cached_tz),
-                                "%c%02d%02d",
-                                t->tm_gmtoff > 0 ? '+' : '-',
-                                abs(t->tm_gmtoff) / 60 / 60,
-                                abs(t->tm_gmtoff) / 60 % 60);
+                                "%+03d%02d", gmtoff_hours, gmtoff_minutes);
                }
 
                if (pp_is_valid(inp, opi->usec_idx)) {