diff options
author | Jeremy Sowden <jeremy@azazel.net> | 2021-11-30 10:55:55 +0000 |
---|---|---|
committer | Pablo Neira Ayuso <pablo@netfilter.org> | 2022-01-03 17:42:43 +0100 |
commit | 9a0487a2b4d8877392d872542a5ada489e454b90 (patch) | |
tree | f017e863ced8b0d0905bb3eb4c9fc2915df042f6 | |
parent | 5cb343adf5c9afa7c72814b8dfb46dc5bc3820d8 (diff) |
output: JSON: fix output of GMT offset
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>
-rw-r--r-- | output/ulogd_output_JSON.c | 9 |
1 files changed, 5 insertions, 4 deletions
diff --git a/output/ulogd_output_JSON.c b/output/ulogd_output_JSON.c index 6edfa90..8234e1b 100644 --- a/output/ulogd_output_JSON.c +++ b/output/ulogd_output_JSON.c @@ -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)) { |