diff options
author | Guy Harris <guy@alum.mit.edu> | 2018-08-05 15:59:44 -0700 |
---|---|---|
committer | Guy Harris <guy@alum.mit.edu> | 2018-08-05 15:59:44 -0700 |
commit | e217f88289b9601e3a9a8d13477f867bcb83b5e2 (patch) | |
tree | 76d6f0e719a8c8a6fbd0db055e0261d3a6c6f48d | |
parent | 90afc7640b08b812ff8e1f112e7dad7f690b0072 (diff) | |
download | libpcap-e217f88289b9601e3a9a8d13477f867bcb83b5e2.tar.gz |
Add PRIsize, to use as the print format for size_t, and use it.
size_t may be bigger than unsigned long, so don't just cast to unsigned
long and print with %lu". (In practice, the value is unlikely to be
bigger, but this is cleaner.)
We could use %zu on UN*X, as we require C99 support and C99 requires
support for z as a size modifier, but MSVC doesn't support z as a size
modifier until Visual Studio 2017.
-rw-r--r-- | pcap/pcap-inttypes.h | 11 | ||||
-rw-r--r-- | savefile.c | 5 | ||||
-rw-r--r-- | sf-pcap.c | 22 | ||||
-rw-r--r-- | sf-pcapng.c | 13 |
4 files changed, 29 insertions, 22 deletions
diff --git a/pcap/pcap-inttypes.h b/pcap/pcap-inttypes.h index af2c23c8..8b1eb8b9 100644 --- a/pcap/pcap-inttypes.h +++ b/pcap/pcap-inttypes.h @@ -106,12 +106,23 @@ #define PRIu64 "llu" #endif #endif + + /* + * MSVC's support library doesn't support %zu to print a size_t until + * Visual Studio 2017, but supports %Iu earlier, so use that. + */ + #define PRIsize "Iu" #elif defined(__MINGW32__) || !defined(_WIN32) /* * Compiler is MinGW or target is UN*X or MS-DOS. Just use * <inttypes.h>. */ #include <inttypes.h> + + /* + * Assume the support library supports %zu; it's required by C99. + */ + #define PRIsize "zu" #endif #endif /* pcap/pcap-inttypes.h */ @@ -365,9 +365,8 @@ pcap_fopen_offline_with_tstamp_precision(FILE *fp, u_int precision, errno, "error reading dump file"); } else { pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, - "truncated dump file; tried to read %lu file header bytes, only got %lu", - (unsigned long)sizeof(magic), - (unsigned long)amt_read); + "truncated dump file; tried to read %" PRIsize " file header bytes, only got %" PRIsize, + sizeof(magic), amt_read); } return (NULL); } @@ -176,9 +176,8 @@ pcap_check_header(bpf_u_int32 magic, FILE *fp, u_int precision, char *errbuf, errno, "error reading dump file"); } else { pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, - "truncated dump file; tried to read %lu file header bytes, only got %lu", - (unsigned long)sizeof(hdr), - (unsigned long)amt_read); + "truncated dump file; tried to read %" PRIsize " file header bytes, only got %" PRIsize, + sizeof(hdr), amt_read); } *err = 1; return (NULL); @@ -449,9 +448,8 @@ pcap_next_packet(pcap_t *p, struct pcap_pkthdr *hdr, u_char **data) } else { if (amt_read != 0) { pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, - "truncated dump file; tried to read %lu header bytes, only got %lu", - (unsigned long)ps->hdrsize, - (unsigned long)amt_read); + "truncated dump file; tried to read %" PRIsize " header bytes, only got %" PRIsize, + ps->hdrsize, amt_read); return (-1); } /* EOF */ @@ -610,8 +608,8 @@ pcap_next_packet(pcap_t *p, struct pcap_pkthdr *hdr, u_char **data) * the read finished. */ pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, - "truncated dump file; tried to read %u captured bytes, only got %lu", - p->snapshot, (unsigned long)amt_read); + "truncated dump file; tried to read %u captured bytes, only got %" PRIsize, + p->snapshot, amt_read); } return (-1); } @@ -634,8 +632,8 @@ pcap_next_packet(pcap_t *p, struct pcap_pkthdr *hdr, u_char **data) "error reading dump file"); } else { pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, - "truncated dump file; tried to read %u captured bytes, only got %lu", - hdr->caplen, (unsigned long)bytes_read); + "truncated dump file; tried to read %u captured bytes, only got %" PRIsize, + hdr->caplen, bytes_read); } return (-1); } @@ -683,8 +681,8 @@ pcap_next_packet(pcap_t *p, struct pcap_pkthdr *hdr, u_char **data) "error reading dump file"); } else { pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, - "truncated dump file; tried to read %u captured bytes, only got %lu", - hdr->caplen, (unsigned long)amt_read); + "truncated dump file; tried to read %u captured bytes, only got %" PRIsize, + hdr->caplen, amt_read); } return (-1); } diff --git a/sf-pcapng.c b/sf-pcapng.c index 970c675c..06c896ac 100644 --- a/sf-pcapng.c +++ b/sf-pcapng.c @@ -261,9 +261,8 @@ read_bytes(FILE *fp, void *buf, size_t bytes_to_read, int fail_on_eof, if (amt_read == 0 && !fail_on_eof) return (0); /* EOF */ pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, - "truncated dump file; tried to read %lu bytes, only got %lu", - (unsigned long)bytes_to_read, - (unsigned long)amt_read); + "truncated dump file; tried to read %" PRIsize " bytes, only got %" PRIsize, + bytes_to_read, amt_read); } return (-1); } @@ -311,9 +310,9 @@ read_block(FILE *fp, pcap_t *p, struct block_cursor *cursor, char *errbuf) if (bhdr.total_length < sizeof(struct block_header) + sizeof(struct block_trailer)) { pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, - "block in pcapng dump file has a length of %u < %lu", + "block in pcapng dump file has a length of %u < %" PRIsize, bhdr.total_length, - (unsigned long)(sizeof(struct block_header) + sizeof(struct block_trailer))); + sizeof(struct block_header) + sizeof(struct block_trailer)); return (-1); } @@ -831,9 +830,9 @@ pcap_ng_check_header(bpf_u_int32 magic, FILE *fp, u_int precision, char *errbuf, */ if (total_length < sizeof(*bhdrp) + sizeof(*shbp) + sizeof(struct block_trailer)) { pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, - "Section Header Block in pcapng dump file has a length of %u < %lu", + "Section Header Block in pcapng dump file has a length of %u < %" PRIsize, total_length, - (unsigned long)(sizeof(*bhdrp) + sizeof(*shbp) + sizeof(struct block_trailer))); + sizeof(*bhdrp) + sizeof(*shbp) + sizeof(struct block_trailer)); *err = 1; return (NULL); } |