[OpenWrt-Devel,1/2] nmea.c: Add null byte to nmea fields
diff mbox series

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

Commit Message

Alexander Couzens June 29, 2018, 9:19 p.m. UTC
Either the main.c treat them as non-null terminated strings or
nmea adds a null byte to the fields.

Found-by: Coverity
Fixes CID 1431321 1431098
---
 nmea.c | 12 ++++++------
 nmea.h |  2 +-
 2 files changed, 7 insertions(+), 7 deletions(-)

Patch
diff mbox series

diff --git a/nmea.c b/nmea.c
index 73ee722dfb7e..242bc21eda59 100644
--- a/nmea.c
+++ b/nmea.c
@@ -51,7 +51,7 @@  struct nmea_param {
 } nmea_params[MAX_NMEA_PARAM];
 
 static int nmea_bad_time;
-char longitude[32] = { 0 }, latitude[32] = { 0 }, course[16] = { 0 }, speed[16] = { 0 }, elevation[16] = { 0 };
+char longitude[33] = { 0 }, latitude[33] = { 0 }, course[17] = { 0 }, speed[17] = { 0 }, elevation[17] = { 0 };
 int gps_valid = 0;
 
 static void
@@ -136,8 +136,8 @@  nmea_rmc_cb(void)
 		if (*nmea_params[6].str == 'W')
 			lon *= -1.0;
 
-		snprintf(latitude, sizeof(latitude), "%f", lat);
-		snprintf(longitude, sizeof(longitude), "%f", lon);
+		snprintf(latitude, sizeof(latitude) - 1, "%f", lat);
+		snprintf(longitude, sizeof(longitude) - 1, "%f", lon);
 
 		DEBUG(3, "position: %s %s\n", latitude, longitude);
 		gps_timestamp();
@@ -149,7 +149,7 @@  nmea_gga_cb(void)
 {
 	if (!gps_valid)
 		return;
-	strncpy(elevation, nmea_params[9].str, sizeof(elevation));
+	strncpy(elevation, nmea_params[9].str, sizeof(elevation) - 1);
 	DEBUG(4, "height: %s\n", elevation);
 }
 
@@ -158,8 +158,8 @@  nmea_vtg_cb(void)
 {
 	if (!gps_valid)
 		return;
-	strncpy(course, nmea_params[1].str, sizeof(course));
-	strncpy(speed, nmea_params[7].str, sizeof(speed));
+	strncpy(course, nmea_params[1].str, sizeof(course) - 1);
+	strncpy(speed, nmea_params[7].str, sizeof(speed) - 1);
 	DEBUG(4, "course: %s\n", course);
 	DEBUG(4, "speed: %s\n", speed);
 }
diff --git a/nmea.h b/nmea.h
index c6f1896df986..9f46d299983e 100644
--- a/nmea.h
+++ b/nmea.h
@@ -23,7 +23,7 @@ 
 
 #include <libubox/ustream.h>
 
-extern char longitude[32], latitude[32], course[16], speed[16], elevation[16];
+extern char longitude[33], latitude[33], course[17], speed[17], elevation[17];
 extern int nmea_open(char *dev, struct ustream_fd *s, speed_t speed);
 extern void gps_timestamp(void);
 extern unsigned int adjust_clock;