From: Guy Harris Date: Mon, 10 Sep 2018 18:43:56 +0000 (-0700) Subject: Clean up -Wshorten-64-to-32 warnings in the test programs. X-Git-Tag: libpcap-1.10-bp~848 X-Git-Url: https://git.tcpdump.org/libpcap/commitdiff_plain/ea4fb6ebfec645988eedb707411e84bb2646d326 Clean up -Wshorten-64-to-32 warnings in the test programs. Clean up some other stuff while we're at it. --- diff --git a/testprogs/filtertest.c b/testprogs/filtertest.c index 7e2d6d6e..17c07e10 100644 --- a/testprogs/filtertest.c +++ b/testprogs/filtertest.c @@ -36,6 +36,7 @@ The Regents of the University of California. All rights reserved.\n"; #include #include #include +#include #ifdef _WIN32 #include "getopt.h" #include "unix.h" @@ -56,6 +57,8 @@ The Regents of the University of California. All rights reserved.\n"; #include "pcap/funcattrs.h" +#define MAXIMUM_SNAPLEN 262144 + #ifdef BDEBUG /* * We have pcap_set_optimizer_debug() and pcap_set_print_dot_graph() in @@ -99,11 +102,21 @@ read_infile(char *fname) if (fstat(fd, &buf) < 0) error("can't stat %s: %s", fname, pcap_strerror(errno)); + /* + * _read(), on Windows, has an unsigned int byte count and an + * int return value, so we can't handle a file bigger than + * INT_MAX - 1 bytes (and have no reason to do so; a filter *that* + * big will take forever to compile). (The -1 is for the '\0' at + * the end of the string.) + */ + if (buf.st_size > INT_MAX - 1) + error("%s is larger than %d bytes; that's too large", fname, + INT_MAX - 1); cp = malloc((u_int)buf.st_size + 1); if (cp == NULL) error("malloc(%d) for %s: %s", (u_int)buf.st_size + 1, fname, pcap_strerror(errno)); - cc = read(fd, cp, (u_int)buf.st_size); + cc = (int)read(fd, cp, (u_int)buf.st_size); if (cc < 0) error("read %s: %s", fname, pcap_strerror(errno)); if (cc != buf.st_size) @@ -199,7 +212,7 @@ main(int argc, char **argv) int gflag; char *infile; int Oflag; - long snaplen; + int snaplen; char *p; int dlt; int have_fcode = 0; @@ -218,7 +231,7 @@ main(int argc, char **argv) infile = NULL; Oflag = 1; - snaplen = 68; + snaplen = MAXIMUM_SNAPLEN; if ((cp = strrchr(argv[0], '/')) != NULL) program_name = cp + 1; @@ -272,13 +285,19 @@ main(int argc, char **argv) case 's': { char *end; + long long_snaplen; - snaplen = strtol(optarg, &end, 0); + long_snaplen = strtol(optarg, &end, 0); if (optarg == end || *end != '\0' - || snaplen < 0 || snaplen > 65535) + || long_snaplen < 0 + || long_snaplen > MAXIMUM_SNAPLEN) error("invalid snaplen %s", optarg); - else if (snaplen == 0) - snaplen = 65535; + else { + if (snaplen == 0) + snaplen = MAXIMUM_SNAPLEN; + else + snaplen = (int)long_snaplen; + } break; } diff --git a/testprogs/opentest.c b/testprogs/opentest.c index bad38eb0..3c83a598 100644 --- a/testprogs/opentest.c +++ b/testprogs/opentest.c @@ -45,7 +45,7 @@ The Regents of the University of California. All rights reserved.\n"; #include "portability.h" #endif -#define MAXIMUM_SNAPLEN 65535 +#define MAXIMUM_SNAPLEN 262144 static char *program_name; @@ -95,13 +95,19 @@ main(int argc, char **argv) case 's': { char *end; + long long_snaplen; - snaplen = strtol(optarg, &end, 0); + long_snaplen = strtol(optarg, &end, 0); if (optarg == end || *end != '\0' - || snaplen < 0 || snaplen > MAXIMUM_SNAPLEN) + || long_snaplen < 0 + || long_snaplen > MAXIMUM_SNAPLEN) error("invalid snaplen %s", optarg); - else if (snaplen == 0) - snaplen = MAXIMUM_SNAPLEN; + else { + if (snaplen == 0) + snaplen = MAXIMUM_SNAPLEN; + else + snaplen = (int)long_snaplen; + } break; } diff --git a/testprogs/valgrindtest.c b/testprogs/valgrindtest.c index 104ef6a9..5de2ec4a 100644 --- a/testprogs/valgrindtest.c +++ b/testprogs/valgrindtest.c @@ -59,6 +59,7 @@ The Regents of the University of California. All rights reserved.\n"; #include #include #include +#include #include #include #include @@ -132,11 +133,21 @@ read_infile(char *fname) if (fstat(fd, &buf) < 0) error("can't stat %s: %s", fname, pcap_strerror(errno)); + /* + * _read(), on Windows, has an unsigned int byte count and an + * int return value, so we can't handle a file bigger than + * INT_MAX - 1 bytes (and have no reason to do so; a filter *that* + * big will take forever to compile). (The -1 is for the '\0' at + * the end of the string.) + */ + if (buf.st_size > INT_MAX - 1) + error("%s is larger than %d bytes; that's too large", fname, + INT_MAX - 1); cp = malloc((u_int)buf.st_size + 1); if (cp == NULL) error("malloc(%d) for %s: %s", (u_int)buf.st_size + 1, fname, pcap_strerror(errno)); - cc = read(fd, cp, (u_int)buf.st_size); + cc = (int)read(fd, cp, (u_int)buf.st_size); if (cc < 0) error("read %s: %s", fname, pcap_strerror(errno)); if (cc != buf.st_size)