]> www.infradead.org Git - mtd-utils.git/commitdiff
mtd-utils: Fix some simple cases of uninitialized value reads
authorDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>
Fri, 24 Jan 2020 22:40:45 +0000 (23:40 +0100)
committerDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>
Sun, 9 Feb 2020 21:13:18 +0000 (22:13 +0100)
This patch modifies the internal helpers to read and parse integers
from sysfs files by initializing them first and removes turns an
obscure "a = open(...) if (a >= 0) {...} if (a == -1) {...}" inside
recv_image into a more straight forward if/else branch.

Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
lib/libmtd.c
lib/libubi.c
misc-utils/recv_image.c

index 564e5c038bf90eb191f598914cc58de0d6e3e7c6..9d8d0e83d5b0656c5cddc33a271fbca0c8d74ccb 100644 (file)
@@ -217,6 +217,7 @@ static int read_hex_ll(const char *file, long long *value)
        }
        buf[rd] = '\0';
 
+       *value = 0;
        if (sscanf(buf, "%llx\n", value) != 1) {
                errmsg("cannot read integer from \"%s\"\n", file);
                errno = EINVAL;
@@ -269,6 +270,7 @@ static int read_pos_ll(const char *file, long long *value)
                goto out_error;
        }
 
+       *value = 0;
        if (sscanf(buf, "%lld\n", value) != 1) {
                errmsg("cannot read integer from \"%s\"\n", file);
                errno = EINVAL;
index 4322a19e6859826e0b9013eb1a5ca1a26d4ec8d3..aaeeb386c6518c30f5b1daedbe75cfb110e8c696 100644 (file)
@@ -94,6 +94,7 @@ static int read_positive_ll(const char *file, long long *value)
        }
        buf[rd] = '\0';
 
+       *value = 0;
        if (sscanf(buf, "%lld\n", value) != 1) {
                errmsg("cannot read integer from \"%s\"\n", file);
                errno = EINVAL;
index 7f6662b38d601497cee94e4767fcc835ba0ee1b2..eeaa2e2d0bcae08bf816540470adbed89bd94de7 100644 (file)
@@ -81,8 +81,7 @@ int main(int argc, char **argv)
                        printf("Receive to MTD device %s with erasesize %d\n",
                               argv[3], meminfo.erasesize);
                }
-       }
-       if (flfd == -1) {
+       } else {
                /* Try again, as if it's a file */
                flfd = open(argv[3], O_CREAT|O_TRUNC|O_RDWR, 0644);
                if (flfd < 0) {