From 333ab199a12c7b060d3a3f4d50a8f73ee4fd5ebd Mon Sep 17 00:00:00 2001 From: =?utf8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Thu, 11 Feb 2021 09:50:49 +0100 Subject: [PATCH] fsck: make sure we don't read an unitialized variable This use on %n was completely unnecessary: fprintf returns the number of characters written. And the issue was that if fprintf failed for whatever reason, it would not process the %n and m would be unitialized. Rework the code a bit to simplify it. Coverity CID#1444708. --- src/fsck/fsck.c | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/fsck/fsck.c b/src/fsck/fsck.c index cd012f0f3ae..94aa31e71ac 100644 --- a/src/fsck/fsck.c +++ b/src/fsck/fsck.c @@ -172,7 +172,7 @@ static int process_progress(int fd, FILE* console) { } for (;;) { - int pass, m; + int pass; unsigned long cur, max; _cleanup_free_ char *device = NULL; double p; @@ -206,18 +206,17 @@ static int process_progress(int fd, FILE* console) { last = t; p = percent(pass, cur, max); - fprintf(console, "\r%s: fsck %3.1f%% complete...\r%n", device, p, &m); - fflush(console); + r = fprintf(console, "\r%s: fsck %3.1f%% complete...\r", device, p); + if (r < 0) + return -EIO; /* No point in continuing if something happend to our output stream */ - if (m > clear) - clear = m; + fflush(console); + clear = MAX(clear, r); } if (clear > 0) { - unsigned j; - fputc('\r', console); - for (j = 0; j < (unsigned) clear; j++) + for (int j = 0; j < clear; j++) fputc(' ', console); fputc('\r', console); fflush(console); -- 2.47.3