]> git.ipfire.org Git - thirdparty/mtr.git/commitdiff
build: fix compiler warnings when for OpenBSD, NetBSD and Solaris 175/head
authorMatt Kimball <matt.kimball@gmail.com>
Fri, 30 Dec 2016 16:32:45 +0000 (16:32 +0000)
committerMatt Kimball <matt.kimball@gmail.com>
Fri, 30 Dec 2016 21:10:28 +0000 (13:10 -0800)
When building for NetBSD, the compiler was warning about
index into an array with a character when using isspace() and
similar macros.

IPPROTO_SCTP is also not defined by NetBSD, so there were
warnings about unused SCTP related variables.

OpenBSD complains about using sprintf rather than snprintf.
It's a good idea to use snprintf, anyway, to avoid buffer overruns,
though I believe in these particular cases sprintf was safe.
Nevertheless, snprintf is now used instead.

Solaris requires strings.h to find index().

Solaris complaints about a missing sentinel pointer unless
the terminating NULL in execl is cast to a "char *".

12 files changed:
packet/cmdparse.c
packet/command.c
packet/deconstruct_unix.c
packet/probe.c
packet/probe_unix.c
packet/wait_unix.c
ui/asn.c
ui/cmdpipe.c
ui/curses.c
ui/dns.c
ui/split.c
ui/utils.c

index 1ce250ad6800bfdce7a1c334a35e08dcb852b5eb..6c1d16a7e60aa96309bee2c96ae451e01eb9159c 100644 (file)
@@ -41,7 +41,7 @@ int tokenize_command(
 
     for (i = 0; command_string[i]; i++) {
         if (on_space) {
-            if (!isspace(command_string[i])) {
+            if (!isspace((unsigned char)command_string[i])) {
                 /*  Take care not to exceed the token array length  */
                 if (token_count >= max_tokens) {
                     return -1;
@@ -51,7 +51,7 @@ int tokenize_command(
                 on_space = 0;
             }
         } else {
-            if (isspace(command_string[i])) {
+            if (isspace((unsigned char)command_string[i])) {
                 command_string[i] = 0;
                 on_space = 1;
             }
index 54c3a4835ac2c38fb8929d54ba169df6b6c7277f..2fa9331b7bd422f23690f7e32c63902a0c2a3190 100644 (file)
@@ -24,6 +24,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <strings.h>
 
 #include "cmdparse.h"
 #include "platform.h"
index 5fa169e0588c7e839192df8c97648d9ad56bc752..bdb7dee6b0440844bcabc00cf1453691c37fa63b 100644 (file)
@@ -110,13 +110,15 @@ void handle_inner_ip4_packet(
         sizeof(struct IPHeader) + sizeof(struct UDPHeader);
     const int ip_tcp_size =
         sizeof(struct IPHeader) + sizeof(struct TCPHeader);
-    const int ip_sctp_size =
-        sizeof(struct IPHeader) + sizeof(struct SCTPHeader);
     const struct ICMPHeader *icmp;
     const struct UDPHeader *udp;
     const struct TCPHeader *tcp;
-    const struct SCTPHeader *sctp;
     int udp_length;
+#ifdef IPPROTO_SCTP
+    const int ip_sctp_size =
+        sizeof(struct IPHeader) + sizeof(struct SCTPHeader);
+    const struct SCTPHeader *sctp;
+#endif
 
     if (ip->protocol == IPPROTO_ICMP) {
         if (packet_length < ip_icmp_size) {
@@ -185,13 +187,15 @@ void handle_inner_ip6_packet(
         sizeof(struct IP6Header) + sizeof(struct UDPHeader);
     const int ip_tcp_size =
         sizeof(struct IP6Header) + sizeof(struct TCPHeader);
-    const int ip_sctp_size =
-        sizeof(struct IPHeader) + sizeof(struct SCTPHeader);
     const struct ICMPHeader *icmp;
     const struct UDPHeader *udp;
     const struct TCPHeader *tcp;
-    const struct SCTPHeader *sctp;
     int udp_length;
+#ifdef IPPROTO_SCTP
+    const int ip_sctp_size =
+        sizeof(struct IPHeader) + sizeof(struct SCTPHeader);
+    const struct SCTPHeader *sctp;
+#endif
 
     if (ip->protocol == IPPROTO_ICMPV6) {
         if (packet_length < ip_icmp_size) {
index aedbdf02488a9de09de4ce67be4831ca515374ae..31f47f3c9a425eae84f71c7793c2434fe0e3e956 100644 (file)
@@ -200,7 +200,8 @@ void format_mpls_string(
     char *append_pos = str;
     const struct mpls_label_t *mpls;
 
-    strcpy(str, "");
+    /*  Start with an empty string  */
+    str[0] = 0;
 
     for (i = 0; i < mpls_count; i++) {
         mpls = &mpls_list[i];
index 3f52575bdee8a541a9d39b731eceb53aaa0bcfb5..e115ffe2d5b324c25eeaf6197f86de3da257a330 100644 (file)
@@ -150,9 +150,9 @@ static
 void check_sctp_support(
     struct net_state_t *net_state)
 {
+#ifdef IPPROTO_SCTP
     int sctp_socket;
 
-#ifdef IPPROTO_SCTP
     sctp_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_SCTP);
     if (sctp_socket != -1) {
         close(sctp_socket);
index 408df82be06e41662b9c1b4429661a63305d601b..c4ca5c5d7647e56de50475593f88a2e50859a433 100644 (file)
@@ -23,6 +23,7 @@
 #include <stdbool.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #include <sys/select.h>
 
 /*
index bbf418041c0e6bfdd4992874b00d3b4c622b5641..4239ad0f0a0823428627ba6beb21748991bc31c8 100644 (file)
--- a/ui/asn.c
+++ b/ui/asn.c
@@ -190,11 +190,14 @@ static char* split_txtrec(struct mtr_ctl *ctl, char *txt_rec) {
 
 #ifdef ENABLE_IPV6
 /* from dns.c:addr2ip6arpa() */
-static void reverse_host6(struct in6_addr *addr, char *buff) {
+static void reverse_host6(struct in6_addr *addr, char *buff, int buff_length) {
     int i;
     char *b = buff;
     for (i=(sizeof(*addr)/2-1); i>=0; i--, b+=4) /* 64b portion */
-        sprintf(b, "%x.%x.", addr->s6_addr[i] & 0xf, addr->s6_addr[i] >> 4);
+        snprintf(
+            b, buff_length,
+            "%x.%x.", addr->s6_addr[i] & 0xf, addr->s6_addr[i] >> 4);
+
     buff[strlen(buff) - 1] = '\0';
 }
 #endif
@@ -210,7 +213,7 @@ static char *get_ipinfo(struct mtr_ctl *ctl, ip_t *addr){
 
     if (ctl->af == AF_INET6) {
 #ifdef ENABLE_IPV6
-        reverse_host6(addr, key);
+        reverse_host6(addr, key, NAMELEN);
         if (snprintf(lookup_key, NAMELEN, "%s.origin6.asn.cymru.com", key) >= NAMELEN)
             return NULL;
 #else
@@ -243,7 +246,7 @@ static char *get_ipinfo(struct mtr_ctl *ctl, ip_t *addr){
             DEB_syslog(LOG_INFO, "Looked up: %s", key);
             if (iihash)
                 if ((item.key = xstrdup(key))) {
-                    item.data = items;
+                    item.data = (void *)items;
                     hsearch(item, ENTER);
                     DEB_syslog(LOG_INFO, "Insert into hash: %s", key);
                 }
index 9ce4ce10e4023d34daae6370eb54ef1f8804403b..81b1e7bc98bbe61d81d5cfeaeb44b92d8191fc91 100644 (file)
@@ -26,6 +26,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <strings.h>
 #include <sys/wait.h>
 #include <unistd.h>
 
@@ -225,7 +226,7 @@ void execute_packet_child(void)
         First, try to execute using /usr/bin/env, because this
         will search the PATH for mtr-packet
     */
-    execl("/usr/bin/env", "mtr-packet", mtr_packet_path, NULL);
+    execl("/usr/bin/env", "mtr-packet", mtr_packet_path, (char *)NULL);
 
     /*
         If env fails to execute, try to use the MTR_PACKET environment
@@ -236,7 +237,7 @@ void execute_packet_child(void)
         could be executed.  This will only be the case if /usr/bin/env
         doesn't exist.
     */
-    execl(mtr_packet_path, "mtr-packet", NULL);
+    execl(mtr_packet_path, "mtr-packet", (char *)NULL);
 
     /*  Both exec attempts failed, so nothing to do but exit  */
     exit(1);
index 94681f035b0045f0557d9defe08ee7b4c7b00e3a..214f27dfbc4ad4f7d7f8d0b2a52c0214bad92c47 100644 (file)
@@ -91,7 +91,7 @@ static void pwcenter(char *str)
 
   getmaxyx(stdscr, __unused_int, maxx);
   cx = (size_t)(maxx - strlen(str)) / 2;
-  printw("%*s%s", cx, "", str);
+  printw("%*s%s", (int)cx, "", str);
 }
 
 
@@ -360,7 +360,7 @@ extern int mtr_curses_keyaction(struct mtr_ctl *ctl)
 }
 
 
-static void format_field (char *dst, const char *format, int n)
+static void format_field (char *dst, int dst_length, const char *format, int n)
 {
   if (index (format, 'N' ) ) {
     *dst++ = ' ';
@@ -368,10 +368,10 @@ static void format_field (char *dst, const char *format, int n)
   } else if (strchr( format, 'f' ) ) {
     /* this is for fields where we measure integer microseconds but
        display floating point miliseconds. Convert to float here. */
-    sprintf(dst, format, n / 1000.0 ); 
+    snprintf(dst, dst_length, format, n / 1000.0 );
     /* this was marked as a temporary hack over 10 years ago. -- REW */
   } else {
-    sprintf(dst, format, n);
+    snprintf(dst, dst_length, format, n);
   } 
 } 
 
@@ -423,7 +423,9 @@ static void mtr_curses_hosts(struct mtr_ctl *ctl, int startstat)
           can't be careful enough. */
        j = ctl->fld_index[ctl->fld_active[i]];
        if (j == -1) continue; 
-        format_field (buf+hd_len, data_fields[j].format, data_fields[j].net_xxx(at));
+        format_field (
+          buf+hd_len, sizeof(buf) - hd_len,
+          data_fields[j].format, data_fields[j].net_xxx(at));
        hd_len +=  data_fields[j].length;
       }
       buf[hd_len] = 0;
@@ -648,8 +650,9 @@ extern void mtr_curses_redraw(struct mtr_ctl *ctl)
        j = ctl->fld_index[ctl->fld_active[i]];
        if (j < 0) continue;
 
-       sprintf( fmt, "%%%ds", data_fields[j].length );
-        sprintf( buf + hd_len, fmt, data_fields[j].title );
+       snprintf( fmt, sizeof(fmt), "%%%ds", data_fields[j].length );
+        snprintf(
+            buf + hd_len, sizeof(buf) - hd_len, fmt, data_fields[j].title );
        hd_len +=  data_fields[j].length;
     }
     attron(A_BOLD);
@@ -673,7 +676,7 @@ extern void mtr_curses_redraw(struct mtr_ctl *ctl)
     max_cols = maxx <= SAVED_PINGS + padding ? maxx-padding : SAVED_PINGS;
     startstat = padding - 2;
 
-    sprintf(msg, " Last %3d pings", max_cols);
+    snprintf(msg, sizeof(msg), " Last %3d pings", max_cols);
     mvprintw(rowstat - 1, startstat, msg);
     
     attroff(A_BOLD);
index e945c0415d4a916d500a7f13d3c6da1d56cd0433..7e976cffc3f5c5f5f2ca3a4822096ddcde304de3 100644 (file)
--- a/ui/dns.c
+++ b/ui/dns.c
@@ -179,7 +179,10 @@ extern void dns_open(struct mtr_ctl *ctl)
         rv = getnameinfo  ((struct sockaddr *) &sa, salen, 
                               hostname, sizeof (hostname), NULL, 0, 0);
         if (rv == 0) {
-          sprintf (result, "%s %s\n", strlongip (ctl, &host), hostname);
+          snprintf (
+            result, sizeof(result),
+            "%s %s\n", strlongip (ctl, &host), hostname);
+
           rv = write (fromdns[1], result, strlen (result));
           if (rv < 0)
             error (0, errno, "write DNS lookup result");
index 1bcce4ee31405de4f253d92771131f4687f1272f..dc4b34e0bfc895e143f03eceb1fcc5d8813ca5d8 100644 (file)
@@ -106,7 +106,7 @@ extern void split_redraw(struct mtr_ctl *ctl)
                net_best(at) /1000, net_avg(at)/1000,
                net_worst(at)/1000);
     } else {
-      sprintf(newLine, "???");
+      snprintf(newLine, sizeof(newLine), "???");
     }
 
     if (strcmp(newLine, Lines[at]) == 0) {
@@ -150,7 +150,7 @@ extern void split_close(void)
 extern int split_keyaction(void) 
 {
 #ifdef HAVE_CURSES
-  char c = getch();
+  unsigned char c = getch();
 #else
   fd_set readfds;
   struct timeval tv;
index 6681038592e516467f7b6d895cada2dcfc969163..af12b1e2eaa8bd232ff28c6598ec1647d87d6b7d 100644 (file)
@@ -46,7 +46,7 @@ extern char *trim(char *str, const char c)
   size_t len;
 
   /* left trim */
-  while (*p && (isspace(*p) || (c && *p == c)))
+  while (*p && (isspace((unsigned char)*p) || (c && *p == c)))
     p++;
   if (str < p) {
     len = strlen(str);
@@ -57,7 +57,7 @@ extern char *trim(char *str, const char c)
   len = strlen(str);
   while (len) {
     len--;
-    if (isspace(str[len]) || (c && str[len] == c)) {
+    if (isspace((unsigned char)str[len]) || (c && str[len] == c)) {
       continue;
     }
     len++;