check timegm return code
authorJohn Crispin <john@phrozen.org>
Thu, 12 Jul 2018 08:05:17 +0000 (10:05 +0200)
committerJohn Crispin <john@phrozen.org>
Thu, 12 Jul 2018 08:05:17 +0000 (10:05 +0200)
Signed-off-by: John Crispin <john@phrozen.org>
nmea.c

diff --git a/nmea.c b/nmea.c
index 4a49528130b70dea4ddea13eb41845f5ac42f022..7793097f105f04530406916b2dcb028b7ccb699c 100644 (file)
--- a/nmea.c
+++ b/nmea.c
@@ -97,12 +97,14 @@ nmea_rmc_cb(void)
                DEBUG(3, "date: %s UTC\n", tmp);
 
                if (adjust_clock) {
-                       struct timeval tv = { timegm(&tm), 0 };
+                       time_t sec = timegm(&tm);
+                       struct timeval tv = { 0 };
                        struct timeval cur;
 
                        gettimeofday(&cur, NULL);
 
-                       if (abs(cur.tv_sec - tv.tv_sec) > MAX_TIME_OFFSET) {
+                       if ((sec < 0) || (abs(cur.tv_sec - tv.tv_sec) > MAX_TIME_OFFSET)) {
+                               tv.tv_sec = sec;
                                if (++nmea_bad_time > MAX_BAD_TIME) {
                                        LOG("system time differs from GPS time by more than %d seconds. Using %s UTC as the new time\n", MAX_TIME_OFFSET, tmp);
                                        /* only set datetime if specified by command line argument! */