From e69b0fe8127ad10059421de6d8f8f0edb366e7f6 Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Fri, 26 Nov 2021 06:21:00 +0100 Subject: [PATCH] Fix #43: avoid assert() on and around 19 January 2038 03:14:07 UTC When time_t wraps around on 32-bit UNIX systems we shouldn't assert (and cause syslogd to be continously restarted) but instead try to handle the wraparound more gracefully. This change, initially proposed by Raul Porancea, checks for wraparound and allows syslogd to continue on error. Logging with invalid date is better than no logs at all. Thanks Raul for tracking this one down! Signed-off-by: Joachim Wiberg --- src/syslogd.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/syslogd.c b/src/syslogd.c index ca857d3..75f4182 100644 --- a/src/syslogd.c +++ b/src/syslogd.c @@ -1013,7 +1013,7 @@ bad: static void parsemsg_rfc3164(const char *from, int pri, char *msg) { - struct logtime timestamp_remote; + struct logtime timestamp_remote = { 0 }; struct buf_msg buffer; struct tm tm_parsed; size_t i, msglen; @@ -1034,8 +1034,8 @@ parsemsg_rfc3164(const char *from, int pri, char *msg) msg += RFC3164_DATELEN + 1; if (!RemoteAddDate) { + time_t t_now, t_remote; struct tm tm_now; - time_t t_now; int year; /* @@ -1053,13 +1053,15 @@ parsemsg_rfc3164(const char *from, int pri, char *msg) t_now = time(NULL); localtime_r(&t_now, &tm_now); for (year = tm_now.tm_year + 1;; --year) { - assert(year >= tm_now.tm_year - 1); + if (year < tm_now.tm_year - 1) + break; timestamp_remote.tm = tm_parsed; timestamp_remote.tm.tm_year = year; timestamp_remote.tm.tm_isdst = -1; timestamp_remote.usec = 0; - if (mktime(×tamp_remote.tm) < - t_now + 7 * 24 * 60 * 60) + t_remote = mktime(×tamp_remote.tm); + if ((t_remote != (time_t)-1) && + (t_remote - t_now) < 7 * 24 * 60 * 60) break; } buffer.timestamp = timestamp_remote;