From: Guy Harris Date: Mon, 10 Sep 2018 03:47:51 +0000 (-0700) Subject: Use -Wshorten-64-to-32 if it's available, and fix warnings it shows. X-Git-Tag: libpcap-1.10-bp~850 X-Git-Url: https://git.tcpdump.org/libpcap/commitdiff_plain/f7e7c2c0ce089ad1ad9a298203c29216fe2b76b1 Use -Wshorten-64-to-32 if it's available, and fix warnings it shows. 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. --- diff --git a/CMakeLists.txt b/CMakeLists.txt index 95a0f88a..497ef607 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/aclocal.m4 b/aclocal.m4 index af34a796..807e74b1 100644 --- a/aclocal.m4 +++ b/aclocal.m4 @@ -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() # diff --git a/configure b/configure index 5d6cd825..37575a49 100755 --- 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 diff --git a/gencode.c b/gencode.c index 42c3bdcb..0f006411 100644 --- 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; diff --git a/pcap-bpf.c b/pcap-bpf.c index 5e9f6899..8a8e8a15 100644 --- a/pcap-bpf.c +++ b/pcap-bpf.c @@ -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 diff --git a/pcap-rpcap.c b/pcap-rpcap.c index 21d4a23e..a35eaf24 100644 --- a/pcap-rpcap.c +++ b/pcap-rpcap.c @@ -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); diff --git a/rpcapd/daemon.c b/rpcapd/daemon.c index e7cef199..27662fd5 100644 --- a/rpcapd/daemon.c +++ b/rpcapd/daemon.c @@ -39,6 +39,7 @@ #include // for the errno variable #include // for malloc(), free(), ... #include // for strlen(), ... +#include // for INT_MAX #ifdef _WIN32 #include // 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, diff --git a/rpcapd/fileconf.c b/rpcapd/fileconf.c index ab1e9dd3..5b896086 100644 --- a/rpcapd/fileconf.c +++ b/rpcapd/fileconf.c @@ -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) { diff --git a/sf-pcap.c b/sf-pcap.c index b3260f9d..9e52a8bb 100644 --- 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; diff --git a/sockutils.h b/sockutils.h index 1df1ef7b..d3299eaf 100644 --- a/sockutils.h +++ b/sockutils.h @@ -53,6 +53,12 @@ * 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