From patchwork Fri Nov 10 11:30:32 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: David Oberhollenzer X-Patchwork-Id: 836701 X-Patchwork-Delegate: david.oberhollenzer@sigma-star.at Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.infradead.org (client-ip=65.50.211.133; helo=bombadil.infradead.org; envelope-from=linux-mtd-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="AENYoYef"; dkim-atps=neutral Received: from bombadil.infradead.org (bombadil.infradead.org [65.50.211.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3yYHtn2rqnz9sBW for ; Fri, 10 Nov 2017 22:32:01 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:Message-Id:Date:Subject:To :From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References: List-Owner; bh=2Sd0Sr6sPRMjZeJP0b2GfmIBzA4gyz2lZp9heGsdAaY=; b=AENYoYefkw4RFB f83ke8UqUgdEsrMj4Z3GGE1mMs2wQgRIpOoQ7KArT5LHqAjZu78GSJIdMpM163ApJR6JQYpq2M3Iq FektjPkCvfWuS2U/gug4Z5odJssOLhJZtDkdWY0pW+r8s61Ibb44551zTrTG0iCcJvt/c/y+2++hi YOX7DkIdxVI8KbEwB+OmRDCkjrucslmfRhg5NVnNc4pP3g6OY/iiol5Caci04rIL1fxHIdjFREf/7 jXJQGYOWAmXs9pFYa/GE1jdkY5Z7yekl/w5JCHt5wZWVALyXsDKGzaEN6kQVlv+I/VH3NJBERWCRf wDibyhGQqcFrWbj7GtZA==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.87 #1 (Red Hat Linux)) id 1eD7XM-0002fo-Jv; Fri, 10 Nov 2017 11:31:52 +0000 Received: from mail.sigma-star.at ([95.130.255.111]) by bombadil.infradead.org with esmtp (Exim 4.87 #1 (Red Hat Linux)) id 1eD7XJ-0002Am-0V for linux-mtd@lists.infradead.org; Fri, 10 Nov 2017 11:31:51 +0000 Received: from localhost (localhost.localdomain [127.0.0.1]) by mail.sigma-star.at (Postfix) with ESMTP id 3D47224E0011; Fri, 10 Nov 2017 12:31:14 +0100 (CET) Received: from linux-4sv6.suse (unknown [82.150.214.1]) by mail.sigma-star.at (Postfix) with ESMTPSA id BAA0F24E0010; Fri, 10 Nov 2017 12:31:13 +0100 (CET) From: David Oberhollenzer To: linux-mtd@lists.infradead.org Subject: [PATCH] ubi-utils: ubicrc32: process command line arguments first Date: Fri, 10 Nov 2017 12:30:32 +0100 Message-Id: <20171110113032.1758-1-david.oberhollenzer@sigma-star.at> X-Mailer: git-send-email 2.12.3 MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20171110_033149_225167_2E11D0E9 X-CRM114-Status: GOOD ( 12.87 ) X-Spam-Score: -1.9 (-) X-Spam-Report: SpamAssassin version 3.4.1 on bombadil.infradead.org summary: Content analysis details: (-1.9 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 SPF_PASS SPF: sender matches SPF record -0.0 RP_MATCHES_RCVD Envelope sender domain matches handover relay domain -1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1% [score: 0.0000] X-BeenThere: linux-mtd@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: Linux MTD discussion mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: ukleinek@debian.org, David Oberhollenzer Sender: "linux-mtd" Errors-To: linux-mtd-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org 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 Signed-off-by: David Oberhollenzer --- ubi-utils/ubicrc32.c | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/ubi-utils/ubicrc32.c b/ubi-utils/ubicrc32.c index 0ea255d..885f348 100644 --- a/ubi-utils/ubicrc32.c +++ b/ubi-utils/ubicrc32.c @@ -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;