From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id E47A9C433EF for ; Wed, 24 Nov 2021 08:16:32 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 4616A82F9D; Wed, 24 Nov 2021 09:16:30 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=denx.de Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=denx.de; s=phobos-20191101; t=1637741790; bh=4U+R2SKYxTg1ECsvl8meyvCewbJACVsZcPx4cGvdJMI=; h=To:cc:From:Subject:In-reply-to:References:Date:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From; b=PpnWW/k3ahTZoR6SmD+n1Lf5H4VcrFDnTkJBBH6pZnfddIJseB2/hGPTWXmn/Afu5 De+KBPsKBdc0NQZ+if3cIf9cg+IwyS5wtYyAdIwQ+tmL1Z+DYm5qVX/wpS06ndETUa 9770U14Z5CAktLw9d2vbTkrYgEZsBjPPa0PO7qbFn1P39d91EGaRI/e/yfA0nb2hVn FuroLFVWy28kCGeUrNrlA5TqS8PXzPc0SWZhUqdshCJ2dsusZWDsWP4jJJPIhKbCF7 r+V5Hpde5XcP3GgFxjib2P+chhjlrIOOrwiRIqpht8L4HQ+UGfAJ4qEN5ewM0NuGFS Oav3vLfTTm1xg== Received: from janitor.denx.de (unknown [62.91.23.180]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (No client certificate requested) (Authenticated sender: noc@denx.de) by phobos.denx.de (Postfix) with ESMTPSA id 433C582F83 for ; Wed, 24 Nov 2021 09:16:28 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=denx.de; s=phobos-20191101; t=1637741788; bh=4U+R2SKYxTg1ECsvl8meyvCewbJACVsZcPx4cGvdJMI=; h=To:cc:From:Subject:In-reply-to:References:Date:From; b=tWOE6wGh5GEOQwwrW2S896MS4P/WpeJ+uulpVFHTEpA8xYARs9CYFMKo9Rs9XLUqi dN9LeJKPfplDvmx8z41ujciUC3Gdzy0g/HGP0eBtooiqPukFp38RRqK+AwqCxjIqIs d/yH42LI9eFC8iiA7zanSMYQTPOyyqPO5NeuEr8rv9JTxSzAm0JLnlkNBeMkeoXtyJ Z2eys9mGFXh7EDL/NMCRzWr09cuE7JJ3+NPA7yPQoxmexW1bdKc61Ikwof6WDPR1Q3 c63tquN3MTlVZkRWJY3p7pFFci6g2vW1LnF+2jfkNkjYxKTAX9QndH3/Y1dWssa04O Pp0RVApCGmB+A== Received: by janitor.denx.de (Postfix, from userid 108) id EE005A021F; Wed, 24 Nov 2021 09:16:27 +0100 (CET) Received: from gemini.denx.de (gemini.denx.de [10.4.0.2]) by janitor.denx.de (Postfix) with ESMTPS id CE1CCA00AA; Wed, 24 Nov 2021 09:16:24 +0100 (CET) Received: from gemini.denx.de (localhost [IPv6:::1]) by gemini.denx.de (Postfix) with ESMTP id 76A021E1813; Wed, 24 Nov 2021 09:16:24 +0100 (CET) To: Mattias Hansson cc: u-boot@lists.denx.de From: Wolfgang Denk Subject: Re: [PATCH] tools/mxsimage: Remove fclose on empty FILE pointer MIME-Version: 1.0 Content-type: text/plain; charset=UTF-8 Content-transfer-encoding: 8bit In-reply-to: <20211123080633.8318-1-hansson.mattias@gmail.com> References: <20211123080633.8318-1-hansson.mattias@gmail.com> Comments: In-reply-to Mattias Hansson message dated "Tue, 23 Nov 2021 09:06:33 +0100." Date: Wed, 24 Nov 2021 09:16:24 +0100 Message-ID: <2787774.1637741784@gemini.denx.de> X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.37 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.2 at phobos.denx.de X-Virus-Status: Clean Dear Mattias Hansson, In message <20211123080633.8318-1-hansson.mattias@gmail.com> you wrote: > If `sb_load_cmdfile()` fails to open the configuration file it will jump > to error handling where the code will try to `fclose()` the FILE pointer > which is NULL causing `mkimage` to segfault. > > This patch removes the `fclose()` since `fopen()` always returns NULL > instead of the file descriptor when failing. > --- > tools/mxsimage.c | 1 - > 1 file changed, 1 deletion(-) > > diff --git a/tools/mxsimage.c b/tools/mxsimage.c > index 002f4b525a..c7bd86ce52 100644 > --- a/tools/mxsimage.c > +++ b/tools/mxsimage.c > @@ -1618,7 +1618,6 @@ static int sb_load_cmdfile(struct sb_image_ctx *ictx) > return 0; > > err_file: > - fclose(fp); > fprintf(stderr, "ERR: Failed to load file \"%s\"\n", > ictx->cfg_filename); > return -EINVAL; The whole code with the goto is ugly. Such an error exit may make sense when used in several places, but here it just makes reading the code more difficult. I suggest to fix this like this: diff --git a/tools/mxsimage.c b/tools/mxsimage.c index 002f4b525a..801aaa62ce 100644 --- a/tools/mxsimage.c +++ b/tools/mxsimage.c @@ -1595,8 +1595,11 @@ static int sb_load_cmdfile(struct sb_image_ctx *ictx) size_t len; fp = fopen(ictx->cfg_filename, "r"); - if (!fp) - goto err_file; + if (!fp) { + fprintf(stderr, "ERR: Failed to load file \"%s\"\n", + ictx->cfg_filename); + return -EINVAL; + } while ((rlen = getline(&line, &len, fp)) > 0) { memset(&cmd, 0, sizeof(cmd)); @@ -1616,12 +1619,6 @@ static int sb_load_cmdfile(struct sb_image_ctx *ictx) fclose(fp); return 0; - -err_file: - fclose(fp); - fprintf(stderr, "ERR: Failed to load file \"%s\"\n", - ictx->cfg_filename); - return -EINVAL; } static int sb_build_tree_from_cfg(struct sb_image_ctx *ictx) Thanks. Best regards, Wolfgang Denk -- DENX Software Engineering GmbH, Managing Director: Wolfgang Denk HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd@denx.de Applying computer technology is simply finding the right wrench to pound in the correct screw.