From 9a0487a2b4d8877392d872542a5ada489e454b90 Mon Sep 17 00:00:00 2001 From: Jeremy Sowden Date: Tue, 30 Nov 2021 10:55:55 +0000 Subject: 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 Signed-off-by: Pablo Neira Ayuso --- output/ulogd_output_JSON.c | 9 +++++---- 1 file 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)) { -- cgit v1.2.3