]> www.infradead.org Git - mtd-utils.git/commitdiff
ubi-utils: ubicrc32: process command line arguments first
authorDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>
Thu, 21 Sep 2017 12:18:04 +0000 (14:18 +0200)
committerDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>
Fri, 3 Nov 2017 18:41:32 +0000 (19:41 +0100)
When a command line option is used (e.g. --version), the tool
tries to open it as a file first, then *uppon success* attempts
to process the command line options (including what it assumed
to be an input file) which is obviously broken.

This patch moves command line processing first and then attempts
to open *the first unprocessed* argument.

Reported-by: Uwe Kleine-König <ukleinek@debian.org>
Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
ubi-utils/ubicrc32.c

index 0ea255da3db714e23f8a68bef58fb28e74c250d3..885f3480313f469726cb6156cde025ca859d2955 100644 (file)
@@ -89,19 +89,18 @@ int main(int argc, char * const argv[])
        int err = 0;
        uint32_t crc = UBI_CRC32_INIT;
        char buf[BUFSIZE];
-       FILE *fp;
-
-       if (argc > 1) {
-               fp = fopen(argv[1], "r");
-               if (!fp)
-                       return sys_errmsg("cannot open \"%s\"", argv[1]);
-       } else
-               fp = stdin;
+       FILE *fp = stdin;
 
        err = parse_opt(argc, argv);
        if (err)
                return err;
 
+       if (optind < argc) {
+               fp = fopen(argv[optind], "r");
+               if (!fp)
+                       return sys_errmsg("cannot open \"%s\"", argv[1]);
+       }
+
        while (!feof(fp)) {
                size_t read;