From: Tymoteusz Blazejczyk Date: Wed, 12 Jun 2019 08:30:32 +0000 (+0200) Subject: Fixed the fread call in the savefile.c file X-Git-Tag: libpcap-1.10-bp~483^2~1 X-Git-Url: https://git.tcpdump.org/libpcap/commitdiff_plain/9157a663d9e845e23697f598994f53f67cfef799 Fixed the fread call in the savefile.c file Currently it was an undefined behavior (UB). It passes wrong parameters to the fread function call (1 byte, 4 elements). It should be 4 bytes and 1 element because the `magic` variable is a single 32-bits integer (4 bytes). ``` bytes_read = fread(pointer, number_of_bytes, number_of_elements, file); ``` On some machines the `fread()` call returned 0 with no error from the `ferror()` call with correct and valid PCAP files. Reference: https://en.cppreference.com/w/c/io/fread --- diff --git a/savefile.c b/savefile.c index 152c9177..e6404e74 100644 --- a/savefile.c +++ b/savefile.c @@ -359,7 +359,7 @@ pcap_fopen_offline_with_tstamp_precision(FILE *fp, u_int precision, * Windows Sniffer, and Microsoft Network Monitor) all have magic * numbers that are unique in their first 4 bytes. */ - amt_read = fread((char *)&magic, 1, sizeof(magic), fp); + amt_read = fread(&magic, sizeof(magic), 1, fp); if (amt_read != sizeof(magic)) { if (ferror(fp)) { pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,