t/read-to-pipe-async: Do not divide by zero
authorBart Van Assche <bvanassche@acm.org>
Sat, 11 Jan 2020 23:42:49 +0000 (15:42 -0800)
committerBart Van Assche <bvanassche@acm.org>
Sun, 12 Jan 2020 02:17:37 +0000 (18:17 -0800)
This patch fixes the following Coverity complaint:

23. zero_return: Function call utime_since(&s, &re) returns 0.
CID 280732 (#2 of 2): Division or modulo by zero (DIVIDE_BY_ZERO)
24. divide_by_zero: In expression bytes * 1000UL * 1000UL / utime_since(&s, &re), division by expression utime_since(&s, &re) which may be zero has undefined behavior.

Signed-off-by: Bart Van Assche <bvanassche@acm.org>
t/read-to-pipe-async.c

index bc7986f7f6604e0cb0c7836f3e0e2cff96bf659c..1370ef084e35bdc0b81df18e11d63f9887ae620d 100644 (file)
@@ -581,6 +581,7 @@ int main(int argc, char *argv[])
        struct reader_thread *rt;
        struct writer_thread *wt;
        unsigned long rate;
+       uint64_t elapsed;
        struct stat sb;
        size_t bytes;
        off_t off;
@@ -684,9 +685,11 @@ int main(int argc, char *argv[])
        show_latencies(&wt->s, "WRITERS");
 
        bytes /= 1024;
-       rate = (bytes * 1000UL * 1000UL) / utime_since(&s, &re);
+       elapsed = utime_since(&s, &re);
+       rate = elapsed ? (bytes * 1000UL * 1000UL) / elapsed : 0;
        fprintf(stderr, "Read rate (KiB/sec) : %lu\n", rate);
-       rate = (bytes * 1000UL * 1000UL) / utime_since(&s, &we);
+       elapsed = utime_since(&s, &we);
+       rate = elapsed ? (bytes * 1000UL * 1000UL) / elapsed : 0;
        fprintf(stderr, "Write rate (KiB/sec): %lu\n", rate);
 
        close(fd);