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

Alexander Couzens lynxis at fe80.eu
Fri Jun 29 17:19:12 EDT 2018


Found-By: Coverity
Fixes CID 1432784
---
 nmea.c | 27 +++++++++++++++++----------
 1 file changed, 17 insertions(+), 10 deletions(-)

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;
+				}
 			}
 		}
 	}
-- 
2.18.0


_______________________________________________
openwrt-devel mailing list
openwrt-devel at lists.openwrt.org
https://lists.openwrt.org/mailman/listinfo/openwrt-devel



More information about the openwrt-devel mailing list