From 18d76d6bda59a9eb0a3f062fac91f481babcbdf4 Mon Sep 17 00:00:00 2001 From: Zbigniew Chyla Date: Wed, 7 Jan 2015 11:04:00 +0100 Subject: Always use sizeof to get array size Don't use constant/expression from an array's definition when referring to its size. Eliminates redundancy and avoids problems when array size changes. The change doesn't affect generated code. --- driver_nmea0183.c | 2 +- driver_ubx.c | 2 +- drivers.c | 2 +- gps2udp.c | 2 +- gpsctl.c | 2 +- gpsd.c | 2 +- gpsmon.c | 2 +- json.c | 2 +- lcdgps.c | 10 +++++----- libgps_core.c | 4 ++-- libgpsd_core.c | 2 +- monitor_italk.c | 2 +- net_ntrip.c | 4 ++-- test_libgps.c | 4 ++-- 14 files changed, 21 insertions(+), 21 deletions(-) diff --git a/driver_nmea0183.c b/driver_nmea0183.c index 30ff3b94..166ba360 100644 --- a/driver_nmea0183.c +++ b/driver_nmea0183.c @@ -1276,7 +1276,7 @@ gps_mask_t nmea_parse(char *sentence, struct gps_device_t * session) /*@ -usedef @*//* splint 3.1.1 seems to have a bug here */ /* make an editable copy of the sentence */ - (void)strlcpy((char *)session->nmea.fieldcopy, sentence, NMEA_MAX); + (void)strlcpy((char *)session->nmea.fieldcopy, sentence, sizeof(session->nmea.fieldcopy) - 1); /* discard the checksum part */ for (p = (char *)session->nmea.fieldcopy; (*p != '*') && (*p >= ' ');) diff --git a/driver_ubx.c b/driver_ubx.c index 25b4ecf2..616dcac7 100644 --- a/driver_ubx.c +++ b/driver_ubx.c @@ -343,7 +343,7 @@ static void ubx_msg_inf(struct gps_device_t *session, unsigned char *buf, size_t if (data_len > MAX_PACKET_LENGTH - 1) data_len = MAX_PACKET_LENGTH - 1; - (void)strlcpy(txtbuf, (char *)buf + UBX_PREFIX_LEN, MAX_PACKET_LENGTH); + (void)strlcpy(txtbuf, (char *)buf + UBX_PREFIX_LEN, sizeof(txtbuf)); txtbuf[data_len] = '\0'; switch (msgid) { case UBX_INF_DEBUG: diff --git a/drivers.c b/drivers.c index 5537553a..60304938 100644 --- a/drivers.c +++ b/drivers.c @@ -805,7 +805,7 @@ static int oceanserver_send(struct gpsd_errout_t *errout, va_start(ap, fmt); (void)vsnprintf(buf, sizeof(buf) - 5, fmt, ap); va_end(ap); - (void)strlcat(buf, "", BUFSIZ); + (void)strlcat(buf, "", sizeof(buf)); status = (int)write(fd, buf, strlen(buf)); (void)tcdrain(fd); if (status == (int)strlen(buf)) { diff --git a/gps2udp.c b/gps2udp.c index 0d08d419..c39004d1 100644 --- a/gps2udp.c +++ b/gps2udp.c @@ -73,7 +73,7 @@ static bool aisonly = false; loctime = localtime (&curtime); /* Print it out in a nice format. */ - (void)strftime (buffer, MAX_TIME_LEN, "%H:%M:%S", loctime); + (void)strftime (buffer, sizeof(buffer), "%H:%M:%S", loctime); return (buffer); } diff --git a/gpsctl.c b/gpsctl.c index e8876b16..b5274f06 100644 --- a/gpsctl.c +++ b/gpsctl.c @@ -84,7 +84,7 @@ static bool gps_query(/*@out@*/struct gps_data_t *gpsdata, (void)vsnprintf(buf, sizeof(buf)-2, fmt, ap); va_end(ap); if (buf[strlen(buf)-1] != '\n') - (void)strlcat(buf, "\n", BUFSIZ); + (void)strlcat(buf, "\n", sizeof(buf)); /*@-usedef@*/ if (write(gpsdata->gps_fd, buf, strlen(buf)) <= 0) { gpsd_report(&context.errout, LOG_ERROR, "gps_query(), write failed\n"); diff --git a/gpsd.c b/gpsd.c index fee3c22c..7d499372 100644 --- a/gpsd.c +++ b/gpsd.c @@ -782,7 +782,7 @@ bool gpsd_add_device(const char *device_name, bool flag_nowait) static char stash[BUFSIZ]; /*@ -temptrans -mayaliasunique @*/ - for (q = p; isprint((unsigned char) *p) && !isspace((unsigned char) *p) && /*@i@*/ (p - q < BUFSIZ - 1); + for (q = p; isprint((unsigned char) *p) && !isspace((unsigned char) *p) && /*@i@*/ (p - q < (ssize_t) sizeof(stash) - 1); p++) continue; (void)memcpy(stash, q, (size_t) (p - q)); diff --git a/gpsmon.c b/gpsmon.c index edadd2b7..c2ab1aa5 100644 --- a/gpsmon.c +++ b/gpsmon.c @@ -686,7 +686,7 @@ static void gpsmon_hook(struct gps_device_t *device, gps_mask_t changed UNUSED) timedelta); /*@+type@*/ - (void)strlcpy(buf, PPSBAR, BUFSIZ); + (void)strlcpy(buf, PPSBAR, sizeof(buf)); session.ppslast = noclobber.timedrift; /* coverity[missing_lock] */ session.ppscount++; diff --git a/json.c b/json.c index 9bccd7ad..f208e747 100644 --- a/json.c +++ b/json.c @@ -95,7 +95,7 @@ static void json_trace(int errlevel, const char *fmt, ...) char buf[BUFSIZ]; va_list ap; - (void)strlcpy(buf, "json: ", BUFSIZ); + (void)strlcpy(buf, "json: ", sizeof(buf)); va_start(ap, fmt); (void)vsnprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), fmt, ap); diff --git a/lcdgps.c b/lcdgps.c index d35cd10c..c30f95a2 100644 --- a/lcdgps.c +++ b/lcdgps.c @@ -189,11 +189,11 @@ static void update_lcd(struct gps_data_t *gpsdata) char *s; s = deg_to_str(deg_type, fabs(gpsdata->fix.latitude)); - snprintf(tmpbuf, 254, "widget_set gpsd one 1 1 {Lat: %s %c}\n", s, (gpsdata->fix.latitude < 0) ? 'S' : 'N'); + snprintf(tmpbuf, sizeof(tmpbuf) - 1, "widget_set gpsd one 1 1 {Lat: %s %c}\n", s, (gpsdata->fix.latitude < 0) ? 'S' : 'N'); send_lcd(tmpbuf); s = deg_to_str(deg_type, fabs(gpsdata->fix.longitude)); - snprintf(tmpbuf, 254, "widget_set gpsd two 1 2 {Lon: %s %c}\n", s, (gpsdata->fix.longitude < 0) ? 'W' : 'E'); + snprintf(tmpbuf, sizeof(tmpbuf) - 1, "widget_set gpsd two 1 2 {Lon: %s %c}\n", s, (gpsdata->fix.longitude < 0) ? 'W' : 'E'); send_lcd(tmpbuf); /* As a pilot, a heading of "0" gives me the heebie-jeebies (ie, 0 @@ -201,7 +201,7 @@ static void update_lcd(struct gps_data_t *gpsdata) track=(int)(gpsdata->fix.track); if (track == 0) track = 360; - snprintf(tmpbuf, 254, "widget_set gpsd three 1 3 {%.1f %s %d deg}\n", + snprintf(tmpbuf, sizeof(tmpbuf) - 1, "widget_set gpsd three 1 3 {%.1f %s %d deg}\n", gpsdata->fix.speed*speedfactor, speedunits, track); send_lcd(tmpbuf); @@ -221,10 +221,10 @@ static void update_lcd(struct gps_data_t *gpsdata) avgclimb=0.0; for(n=0;nfix.altitude*altfactor), altunits, gridsquare, (int)(avgclimb * METERS_TO_FEET * 60)); } else { - snprintf(tmpbuf, 254, "widget_set gpsd four 1 4 {n/a}\n"); + snprintf(tmpbuf, sizeof(tmpbuf) - 1, "widget_set gpsd four 1 4 {n/a}\n"); } send_lcd(tmpbuf); } diff --git a/libgps_core.c b/libgps_core.c index 9baeefd4..64bce379 100644 --- a/libgps_core.c +++ b/libgps_core.c @@ -40,7 +40,7 @@ void libgps_trace(int errlevel, const char *fmt, ...) char buf[BUFSIZ]; va_list ap; - (void)strlcpy(buf, "libgps: ", BUFSIZ); + (void)strlcpy(buf, "libgps: ", sizeof(buf)); va_start(ap, fmt); (void)vsnprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), fmt, ap); @@ -178,7 +178,7 @@ int gps_send(struct gps_data_t *gpsdata CONDITIONALLY_UNUSED, const char *fmt CO (void)vsnprintf(buf, sizeof(buf) - 2, fmt, ap); va_end(ap); if (buf[strlen(buf) - 1] != '\n') - (void)strlcat(buf, "\n", BUFSIZ); + (void)strlcat(buf, "\n", sizeof(buf)); #ifdef SOCKET_EXPORT_ENABLE status = gps_sock_send(gpsdata, buf); diff --git a/libgpsd_core.c b/libgpsd_core.c index b24c517f..e96cec63 100644 --- a/libgpsd_core.c +++ b/libgpsd_core.c @@ -1015,7 +1015,7 @@ int gpsd_await_data(/*@out@*/fd_set *rfds, sizeof(dbuf) - strlen(dbuf), "%d ", i); if (strlen(dbuf) > 0) dbuf[strlen(dbuf) - 1] = '\0'; - (void)strlcat(dbuf, "} -> {", BUFSIZ); + (void)strlcat(dbuf, "} -> {", sizeof(dbuf)); for (i = 0; i < FD_SETSIZE; i++) if (FD_ISSET(i, rfds)) (void)snprintf(dbuf + strlen(dbuf), diff --git a/monitor_italk.c b/monitor_italk.c index 1f62b54b..946d706d 100644 --- a/monitor_italk.c +++ b/monitor_italk.c @@ -169,7 +169,7 @@ static void display_itk_navfix(unsigned char *buf, size_t len) for (i = 0; i < 32; i++) { if (svlist & (1 << i)) { (void)snprintf(prn, 4, "%u ", i + 1); - (void)strlcat(satlist, prn, 38); + (void)strlcat(satlist, prn, sizeof(satlist)); } } (void)wprintw(navfixwin, "%02d = %-38s", nsv, satlist); diff --git a/net_ntrip.c b/net_ntrip.c index 47bdb636..be8db7ca 100644 --- a/net_ntrip.c +++ b/net_ntrip.c @@ -164,7 +164,7 @@ static int ntrip_sourcetable_parse(struct gps_device_t *device) bool sourcetable = false; bool match = false; char buf[BUFSIZ]; - size_t blen = BUFSIZ; + size_t blen = sizeof(buf); int fd = device->gpsdata.gps_fd; for (;;) { @@ -547,7 +547,7 @@ int ntrip_open(struct gps_device_t *device, char *caster) return ret; } (void)close(device->gpsdata.gps_fd); - if (ntrip_auth_encode(&device->ntrip.stream, device->ntrip.stream.credentials, device->ntrip.stream.authStr, 128) != 0) { + if (ntrip_auth_encode(&device->ntrip.stream, device->ntrip.stream.credentials, device->ntrip.stream.authStr, sizeof(device->ntrip.stream.authStr)) != 0) { device->ntrip.conn_state = ntrip_conn_err; return -1; } diff --git a/test_libgps.c b/test_libgps.c index 616a2d1c..1dfb15a1 100644 --- a/test_libgps.c +++ b/test_libgps.c @@ -79,8 +79,8 @@ int main(int argc, char *argv[]) (void)fputs("Daemon is not running.\n", stdout); exit(EXIT_FAILURE); } else if (optind < argc) { - (void)strlcpy(buf, argv[optind], BUFSIZ); - (void)strlcat(buf, "\n", BUFSIZ); + (void)strlcpy(buf, argv[optind], sizeof(buf)); + (void)strlcat(buf, "\n", sizeof(buf)); (void)gps_send(&collect, buf); (void)gps_read(&collect); libgps_dump_state(&collect); -- cgit v1.2.1