Fix return value checking of fread() in iolog.c
authorRebecca Cran <rebecca@bluestop.org>
Wed, 4 Apr 2018 23:18:42 +0000 (17:18 -0600)
committerJens Axboe <axboe@kernel.dk>
Thu, 5 Apr 2018 01:18:50 +0000 (19:18 -0600)
According to http://pubs.opengroup.org/onlinepubs/7908799/xsh/fread.html
fread() returns a size_t, not ssize_t, and returns a value of 0 on
both eof and an error. Therefore, check both feof() and ferror() to
determine whether the call succeeded.

Signed-off-by: Jens Axboe <axboe@kernel.dk>
iolog.c

diff --git a/iolog.c b/iolog.c
index 2b5eaf0c52dac715e971e26915d3015f9c274c2c..bfafc032f4bc63dc03964d4b050d2a0089723916 100644 (file)
--- a/iolog.c
+++ b/iolog.c
@@ -978,7 +978,7 @@ int iolog_file_inflate(const char *file)
        struct iolog_compress ic;
        z_stream stream;
        struct stat sb;
-       ssize_t ret;
+       size_t ret;
        size_t total;
        void *buf;
        FILE *f;
@@ -1000,12 +1000,12 @@ int iolog_file_inflate(const char *file)
        ic.seq = 1;
 
        ret = fread(ic.buf, ic.len, 1, f);
-       if (ret < 0) {
+       if (ret == 0 && ferror(f)) {
                perror("fread");
                fclose(f);
                free(buf);
                return 1;
-       } else if (ret != 1) {
+       } else if (ferror(f) || (!feof(f) && ret != 1)) {
                log_err("fio: short read on reading log\n");
                fclose(f);
                free(buf);