I realized that there are probably other places in the code where fread() is called without complete error checking. I also realized that, according to the man page, fread() does not set errno so the use of strerror() (and previously perror()) is incorrect.
This patch checks the return value from fread() every where it is called and prints a message if an error occurred, or returns success if we hit EOF (and it is reasonable to hit EOF). Specific changes:
packet_metadata() -- print an error message and return -1 if unable to read the packet header (previously it silently returned success even if the header was not read).
ctf_trace_metadata_packet_read() -- print a message if an error occurred reading the header and return EINVAL. if we hit EOF return success without a message (previously returned EINVAL without a message for both end-of-file and errors().
ctf_trace_metadata_packet_read() -- in the "big loop", print an error message and return EINVAL if we got an error reading the file or if we hit unexpected EOF. previously no message was printed. previously, if we hit EOF the loop would have continued forever. i also had to rearrange the code a bit to handle the case where "toread" was 0 when the loop was entered.
import_stream_packet_index() -- fix the originally reported problem. Messages differentiate between an i/o error and a file that's too short, but no longer expects errno to be set.
import_stream_packet_index() -- add a check to see if an i/o error caused the loop to terminate; print a message and return an error if so.