diff mbox series

[OpenWrt-Devel,2/2] nmea.c: check return value of timegm() for errors

Message ID 20180629211912.27006-2-lynxis@fe80.eu
State Superseded
Delegated to: John Crispin
Headers show
Series [OpenWrt-Devel,1/2] nmea.c: Add null byte to nmea fields | expand

Commit Message

Alexander 'lynxis' Couzens June 29, 2018, 9:19 p.m. UTC
Found-By: Coverity
Fixes CID 1432784
---
 nmea.c | 27 +++++++++++++++++----------
 1 file changed, 17 insertions(+), 10 deletions(-)
diff mbox series

Patch

diff --git a/nmea.c b/nmea.c
index 242bc21eda59..8bd91bb35f9a 100644
--- a/nmea.c
+++ b/nmea.c
@@ -97,19 +97,26 @@  nmea_rmc_cb(void)
 		DEBUG(3, "date: %s UTC\n", tmp);
 
 		if (adjust_clock) {
-			struct timeval tv = { timegm(&tm), 0 };
+			struct timeval tv = { 0 };
 			struct timeval cur;
+			time_t sec = timegm(&tm);
 
-			gettimeofday(&cur, NULL);
-
-			if (abs(cur.tv_sec - tv.tv_sec) > MAX_TIME_OFFSET) {
-				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! */
-					settimeofday(&tv, NULL);
-				}
+			if (sec == -1) {
+				ERROR("failed to convert time via timegm()");
 			} else {
-				nmea_bad_time = 0;
+				tv.tv_sec = sec;
+
+				gettimeofday(&cur, NULL);
+
+				if (abs(cur.tv_sec - tv.tv_sec) > MAX_TIME_OFFSET) {
+					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! */
+						settimeofday(&tv, NULL);
+					}
+				} else {
+					nmea_bad_time = 0;
+				}
 			}
 		}
 	}