]> The Tcpdump Group git mirrors - libpcap/commitdiff
Fixed the fread call in the savefile.c file
authorTymoteusz Blazejczyk <[email protected]>
Wed, 12 Jun 2019 08:30:32 +0000 (10:30 +0200)
committerTymoteusz Blazejczyk <[email protected]>
Wed, 12 Jun 2019 08:30:32 +0000 (10:30 +0200)
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

savefile.c

index 152c91779089d09925fceb3cf75a20a9cdc2b162..e6404e74a9b49d785891d755636d9672a7f90a0f 100644 (file)
@@ -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,