Check for errors or short files on every call to fread() and fwrite() and return errors as appropriate. Also, print a message if an error is encountered. --- a/formats/ctf/ctf.c +++ b/formats/ctf/ctf.c @@ -1022,6 +1022,8 @@ len = fread(&magic, sizeof(magic), 1, fp); if (len != 1) { + fprintf(stderr, "[error] error reading packet metadata\n"); + ret = -1; goto end; } if (magic == TSDL_MAGIC) { @@ -1075,8 +1077,13 @@ int ret = 0; readlen = fread(&header, header_sizeof(header), 1, in); - if (readlen < 1) + if (readlen < 1) { + if (feof(in) ) { + return 0; + } + fprintf(stderr, "[error] error reading packet header\n"); return -EINVAL; + } if (td->byte_order != BYTE_ORDER) { header.magic = GUINT32_SWAP_LE_BE(header.magic); @@ -1117,8 +1124,14 @@ toread = (header.content_size / CHAR_BIT) - header_sizeof(header); for (;;) { + if (toread == 0) { + ret = 0; /* continue reading next packet */ + goto read_padding; + } + readlen = fread(buf, sizeof(char), min(sizeof(buf) - 1, toread), in); - if (ferror(in)) { + if (readlen == 0 || ferror(in) ) { + fprintf(stderr, "[error] error reading packet\n"); ret = -EINVAL; break; } @@ -1130,6 +1143,7 @@ writelen = fwrite(buf, sizeof(char), readlen, out); if (writelen < readlen) { + fprintf(stderr, "[error] error writing packet\n"); ret = -EIO; break; } @@ -1138,10 +1152,6 @@ break; } toread -= readlen; - if (!toread) { - ret = 0; /* continue reading next packet */ - goto read_padding; - } } return ret; @@ -1842,7 +1852,14 @@ len = fread(&index_hdr, sizeof(index_hdr), 1, pos->index_fp); if (len != 1) { - perror("read index file header"); + char *msg = "File is too short"; + + if (ferror(pos->index_fp) ) { + msg = "I/O error"; + } + fprintf(stderr, "[error] error reading a '*.idx' file under %s: %s\n", + td->parent.path, msg); + ret = -1; goto error; } @@ -1913,6 +1930,12 @@ /* add index to packet array */ g_array_append_val(file_stream->pos.packet_index, index); } + if (ferror(pos->index_fp) ) { + fprintf(stderr, "[error] error reading a '*.idx' file under %s: I/O error\n", + td->parent.path); + ret = -1; + goto error; + } /* Index containing only the header. */ if (!file_stream->parent.stream_class) {