]> The Tcpdump Group git mirrors - libpcap/commitdiff
Use -Wshorten-64-to-32 if it's available, and fix warnings it shows.
authorGuy Harris <[email protected]>
Mon, 10 Sep 2018 03:47:51 +0000 (20:47 -0700)
committerGuy Harris <[email protected]>
Mon, 10 Sep 2018 03:47:51 +0000 (20:47 -0700)
Probably harmless, but do it to help catch some issues on LP64 (64-bit
UN*X) or LLP64 (64-bit Windows) platforms when compiling on macOS.

CMakeLists.txt
aclocal.m4
configure
gencode.c
pcap-bpf.c
pcap-rpcap.c
rpcapd/daemon.c
rpcapd/fileconf.c
sf-pcap.c
sockutils.h

index 95a0f88a5921885513e35effff438737dc605468..497ef6078906833c2bf1277bc2803b17951ac4bd 100644 (file)
@@ -1676,6 +1676,7 @@ if(EXISTS ${CMAKE_SOURCE_DIR}/.devel OR EXISTS ${CMAKE_BINARY_DIR}/.devel)
         check_and_add_compiler_option(-Wmissing-variable-declarations)
         check_and_add_compiler_option(-Wshadow)
         check_and_add_compiler_option(-Wsign-compare)
+        check_and_add_compiler_option(-Wshorten-64-to-32)
         check_and_add_compiler_option(-Wstrict-prototypes)
         check_and_add_compiler_option(-Wunreachable-code)
         check_and_add_compiler_option(-Wunused-parameter)
index af34a796f786de9a4d1077be2670f78f99eeab6e..807e74b16db25117eb93094d4bdd9d7e294b0ce9 100644 (file)
@@ -830,6 +830,7 @@ testme(unsigned short a)
 }
                      ],
                      [generates warnings from ntohs()])
+                   AC_LBL_CHECK_COMPILER_OPT($1, -Wshorten-64-to-32)
            fi
            AC_LBL_CHECK_DEPENDENCY_GENERATION_OPT()
            #
index 5d6cd825da30eadf540f6dbcac2a5805ababea48..37575a4903dd455ffb4903025a7f1effa9b31d67 100755 (executable)
--- a/configure
+++ b/configure
@@ -9767,6 +9767,88 @@ else
 $as_echo "no" >&6; }
                CFLAGS="$save_CFLAGS"
 
+fi
+rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
+
+
+       { $as_echo "$as_me:${as_lineno-$LINENO}: checking whether the compiler supports the -Wshorten-64-to-32 option" >&5
+$as_echo_n "checking whether the compiler supports the -Wshorten-64-to-32 option... " >&6; }
+       save_CFLAGS="$CFLAGS"
+       if expr "x-Wshorten-64-to-32" : "x-W.*" >/dev/null
+       then
+           CFLAGS="$CFLAGS $ac_lbl_unknown_warning_option_error -Wshorten-64-to-32"
+       elif expr "x-Wshorten-64-to-32" : "x-f.*" >/dev/null
+       then
+           CFLAGS="$CFLAGS -Werror -Wshorten-64-to-32"
+       elif expr "x-Wshorten-64-to-32" : "x-m.*" >/dev/null
+       then
+           CFLAGS="$CFLAGS -Werror -Wshorten-64-to-32"
+       else
+           CFLAGS="$CFLAGS -Wshorten-64-to-32"
+       fi
+       cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h.  */
+
+int
+main ()
+{
+return 0
+  ;
+  return 0;
+}
+_ACEOF
+if ac_fn_c_try_compile "$LINENO"; then :
+
+               { $as_echo "$as_me:${as_lineno-$LINENO}: result: yes" >&5
+$as_echo "yes" >&6; }
+               can_add_to_cflags=yes
+               #
+               # The compile supports this; do we have some C code for
+               # which the warning should *not* appear?
+               # We test the fourth argument because the third argument
+               # could contain quotes, breaking the test.
+               #
+               if test "x" != "x"
+               then
+                   CFLAGS="$CFLAGS $ac_lbl_cc_force_warning_errors"
+                   { $as_echo "$as_me:${as_lineno-$LINENO}: checking whether -Wshorten-64-to-32 " >&5
+$as_echo_n "checking whether -Wshorten-64-to-32 ... " >&6; }
+                   cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h.  */
+
+_ACEOF
+if ac_fn_c_try_compile "$LINENO"; then :
+
+                       #
+                       # Not a problem.
+                       #
+                       { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5
+$as_echo "no" >&6; }
+
+else
+
+                       #
+                       # A problem.
+                       #
+                       { $as_echo "$as_me:${as_lineno-$LINENO}: result: yes" >&5
+$as_echo "yes" >&6; }
+                       can_add_to_cflags=no
+
+fi
+rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
+               fi
+               CFLAGS="$save_CFLAGS"
+               if test x"$can_add_to_cflags" = "xyes"
+               then
+                   V_CCOPT="$V_CCOPT -Wshorten-64-to-32"
+               fi
+
+else
+
+               { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5
+$as_echo "no" >&6; }
+               CFLAGS="$save_CFLAGS"
+
 fi
 rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
 
index 42c3bdcbd0835237ff6c5ec4958e1b33cc213b20..0f00641129bef10e41f6ab9302cf17c26ee9c296 100644 (file)
--- a/gencode.c
+++ b/gencode.c
@@ -6181,13 +6181,13 @@ gen_proto(compiler_state_t *cstate, int v, int proto, int dir)
                         */
                        b0 = gen_linktype(cstate, LLCSAP_ISONS<<8 | LLCSAP_ISONS);
                        /* OSI in C-HDLC is stuffed with a fudge byte */
-                       b1 = gen_cmp(cstate, OR_LINKPL_NOSNAP, 1, BPF_B, (long)v);
+                       b1 = gen_cmp(cstate, OR_LINKPL_NOSNAP, 1, BPF_B, (bpf_int32)v);
                        gen_and(b0, b1);
                        return b1;
 
                default:
                        b0 = gen_linktype(cstate, LLCSAP_ISONS);
-                       b1 = gen_cmp(cstate, OR_LINKPL_NOSNAP, 0, BPF_B, (long)v);
+                       b1 = gen_cmp(cstate, OR_LINKPL_NOSNAP, 0, BPF_B, (bpf_int32)v);
                        gen_and(b0, b1);
                        return b1;
                }
@@ -6198,7 +6198,7 @@ gen_proto(compiler_state_t *cstate, int v, int proto, int dir)
                 * 4 is the offset of the PDU type relative to the IS-IS
                 * header.
                 */
-               b1 = gen_cmp(cstate, OR_LINKPL_NOSNAP, 4, BPF_B, (long)v);
+               b1 = gen_cmp(cstate, OR_LINKPL_NOSNAP, 4, BPF_B, (bpf_int32)v);
                gen_and(b0, b1);
                return b1;
 
index 5e9f68992eede21a475dc5eec487d8c13e176120..8a8e8a153c78d446922759a7021c61a9e33ece2a 100644 (file)
@@ -974,7 +974,7 @@ pcap_read_bpf(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
                } else
 #endif
                {
-                       cc = read(p->fd, p->buffer, p->bufsize);
+                       cc = (int)read(p->fd, p->buffer, p->bufsize);
                }
                if (cc < 0) {
                        /* Don't choke when we get ptraced */
@@ -1076,7 +1076,7 @@ pcap_read_bpf(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
                 */
                if (p->break_loop) {
                        p->bp = bp;
-                       p->cc = ep - bp;
+                       p->cc = (int)(ep - bp);
                        /*
                         * ep is set based on the return value of read(),
                         * but read() from a BPF device doesn't necessarily
@@ -1166,7 +1166,7 @@ pcap_read_bpf(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
                        bp += BPF_WORDALIGN(caplen + hdrlen);
                        if (++n >= cnt && !PACKET_COUNT_IS_UNLIMITED(cnt)) {
                                p->bp = bp;
-                               p->cc = ep - bp;
+                               p->cc = (int)(ep - bp);
                                /*
                                 * See comment above about p->cc < 0.
                                 */
@@ -1189,7 +1189,17 @@ pcap_read_bpf(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
 static int
 pcap_inject_bpf(pcap_t *p, const void *buf, size_t size)
 {
-       int ret;
+       ssize_t ret;
+
+       /*
+        * We return the number of bytes written, so the number of
+        * bytes to write must fit in an int.
+        */
+       if (size > INT_MAX) {
+               pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
+                   errno, "More than %d bytes cannot be injected", INT_MAX);
+               return (PCAP_ERROR);
+       }
 
        ret = write(p->fd, buf, size);
 #ifdef __APPLE__
@@ -1231,7 +1241,7 @@ pcap_inject_bpf(pcap_t *p, const void *buf, size_t size)
                    errno, "send");
                return (PCAP_ERROR);
        }
-       return (ret);
+       return ((int)ret);
 }
 
 #ifdef _AIX
index 21d4a23ea5a6e642765c6fe94e27c777f163f6ad..a35eaf240669b500d1f263e00a6c160fbd8cdca6 100644 (file)
@@ -388,7 +388,7 @@ static int pcap_read_nocb_remote(pcap_t *p, struct pcap_pkthdr *pkt_header, u_ch
         * 'timeout', in pcap_t, is in milliseconds; we have to convert it into sec and microsec
         */
        tv.tv_sec = p->opt.timeout / 1000;
-       tv.tv_usec = (p->opt.timeout - tv.tv_sec * 1000) * 1000;
+       tv.tv_usec = (suseconds_t)((p->opt.timeout - tv.tv_sec * 1000) * 1000);
 
        /* Watch out sockdata to see if it has input */
        FD_ZERO(&rfds);
index e7cef199b2e890db80ed50aea8b0e921f745adb1..27662fd5cd156af1563a3b2931587e73ac95e03d 100644 (file)
@@ -39,6 +39,7 @@
 #include <errno.h>             // for the errno variable
 #include <stdlib.h>            // for malloc(), free(), ...
 #include <string.h>            // for strlen(), ...
+#include <limits.h>            // for INT_MAX
 
 #ifdef _WIN32
   #include <process.h>         // for threads
@@ -2270,7 +2271,21 @@ daemon_thrdatamain(void *ptr)
        //
        // So we don't need to make sure that sendbufsize will overflow.
        //
+       // However, we *do* need to make sure its value fits in an int,
+       // because sock_send() can't send more than INT_MAX bytes (it could
+       // do so on 64-bit UN*Xes, but can't do so on Windows, not even
+       // 64-bit Windows, as the send() buffer size argument is an int
+       // in Winsock).
+       //
        sendbufsize = sizeof(struct rpcap_header) + sizeof(struct rpcap_pkthdr) + pcap_snapshot(session->fp);
+       if (sendbufsize > INT_MAX)
+       {
+               rpcapd_log(LOGPRIO_ERROR,
+                   "Buffer size for this child thread would be larger than %d",
+                   INT_MAX);
+               sendbuf = NULL; // we haven't allocated a buffer, so nothing to free
+               goto error;
+       }
        sendbuf = (char *) malloc (sendbufsize);
        if (sendbuf == NULL)
        {
@@ -2311,7 +2326,7 @@ daemon_thrdatamain(void *ptr)
 
                // Bufferize the general header
                if (sock_bufferize(NULL, sizeof(struct rpcap_header), NULL,
-                   &sendbufidx, sendbufsize, SOCKBUF_CHECKONLY, errbuf,
+                   &sendbufidx, (int)sendbufsize, SOCKBUF_CHECKONLY, errbuf,
                    PCAP_ERRBUF_SIZE) == -1)
                {
                        rpcapd_log(LOGPRIO_ERROR,
@@ -2328,7 +2343,7 @@ daemon_thrdatamain(void *ptr)
 
                // Bufferize the pkt header
                if (sock_bufferize(NULL, sizeof(struct rpcap_pkthdr), NULL,
-                   &sendbufidx, sendbufsize, SOCKBUF_CHECKONLY, errbuf,
+                   &sendbufidx, (int)sendbufsize, SOCKBUF_CHECKONLY, errbuf,
                    PCAP_ERRBUF_SIZE) == -1)
                {
                        rpcapd_log(LOGPRIO_ERROR,
@@ -2345,7 +2360,7 @@ daemon_thrdatamain(void *ptr)
 
                // Bufferize the pkt data
                if (sock_bufferize((char *) pkt_data, pkt_header->caplen,
-                   sendbuf, &sendbufidx, sendbufsize, SOCKBUF_BUFFERIZE,
+                   sendbuf, &sendbufidx, (int)sendbufsize, SOCKBUF_BUFFERIZE,
                    errbuf, PCAP_ERRBUF_SIZE) == -1)
                {
                        rpcapd_log(LOGPRIO_ERROR,
index ab1e9dd38982d8e36fbbaae4e7edfb8d511a45ba..5b8960862622e74c9c54c17154d2cb4b0e992808 100644 (file)
@@ -202,7 +202,7 @@ static int strrem(char *string, char chr)
 {
        char *pos;
        int num = 0;
-       int len, i;
+       size_t len, i;
 
        while ((pos = strchr(string, chr)) != NULL)
        {
index b3260f9d69f843965b594d361e8c32b2b3998faf..9e52a8bb68bb0bfe6f71f25cef185bd8a93d8e03 100644 (file)
--- a/sf-pcap.c
+++ b/sf-pcap.c
@@ -731,7 +731,11 @@ pcap_dump(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
        struct pcap_sf_pkthdr sf_hdr;
 
        f = (FILE *)user;
-       sf_hdr.ts.tv_sec  = h->ts.tv_sec;
+       /*
+        * Better not try writing pcap files after
+        * 2038-01-19 03:14:07 UTC; switch to pcapng.
+        */
+       sf_hdr.ts.tv_sec  = (bpf_int32)h->ts.tv_sec;
        sf_hdr.ts.tv_usec = h->ts.tv_usec;
        sf_hdr.caplen     = h->caplen;
        sf_hdr.len        = h->len;
index 1df1ef7bb3ac39cd671543b9a1af4abab313309a..d3299eaf22acddcee468ec525b4d74f438654db0 100644 (file)
    * don't have it, either?
    */
   typedef int socklen_t;
+
+  /*
+   * Winsock doesn't have this POSIX type; it's used for the
+   * tv_usec value of struct timeval.
+   */
+  typedef long suseconds_t;
 #else
   /* UN*X */
   #include <stdio.h>