]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
fsck: make sure we don't read an unitialized variable
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Thu, 11 Feb 2021 08:50:49 +0000 (09:50 +0100)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Thu, 11 Feb 2021 08:50:49 +0000 (09:50 +0100)
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

index cd012f0f3ae49dec94cb6ef68f663e0ab10ec3ae..94aa31e71ac11c0f3b08aaae57df78cad61f6e22 100644 (file)
@@ -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);