From patchwork Fri Feb 12 03:08:27 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Konrad Rzeszutek Wilk X-Patchwork-Id: 8287371 Return-Path: X-Original-To: patchwork-xen-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id B8A55BEEE5 for ; Fri, 12 Feb 2016 03:11:56 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id CF19C203AC for ; Fri, 12 Feb 2016 03:11:55 +0000 (UTC) Received: from lists.xen.org (lists.xenproject.org [50.57.142.19]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id D17F320351 for ; Fri, 12 Feb 2016 03:11:54 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=lists.xen.org) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1aU46C-0006XM-0x; Fri, 12 Feb 2016 03:08:48 +0000 Received: from mail6.bemta14.messagelabs.com ([193.109.254.103]) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1aU46A-0006Wn-Eu for xen-devel@lists.xenproject.org; Fri, 12 Feb 2016 03:08:46 +0000 Received: from [193.109.254.147] by server-13.bemta-14.messagelabs.com id 54/04-08347-DBC4DB65; Fri, 12 Feb 2016 03:08:45 +0000 X-Env-Sender: konrad.wilk@oracle.com X-Msg-Ref: server-11.tower-27.messagelabs.com!1455246522!14627500!1 X-Originating-IP: [156.151.31.81] X-SpamReason: No, hits=0.0 required=7.0 tests=sa_preprocessor: VHJ1c3RlZCBJUDogMTU2LjE1MS4zMS44MSA9PiAyODgzMzk=\n X-StarScan-Received: X-StarScan-Version: 7.35.1; banners=-,-,- X-VirusChecked: Checked Received: (qmail 16161 invoked from network); 12 Feb 2016 03:08:44 -0000 Received: from userp1040.oracle.com (HELO userp1040.oracle.com) (156.151.31.81) by server-11.tower-27.messagelabs.com with DHE-RSA-AES256-GCM-SHA384 encrypted SMTP; 12 Feb 2016 03:08:44 -0000 Received: from aserv0021.oracle.com (aserv0021.oracle.com [141.146.126.233]) by userp1040.oracle.com (Sentrion-MTA-4.3.2/Sentrion-MTA-4.3.2) with ESMTP id u1C38d6S010928 (version=TLSv1 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Fri, 12 Feb 2016 03:08:40 GMT Received: from aserv0121.oracle.com (aserv0121.oracle.com [141.146.126.235]) by aserv0021.oracle.com (8.13.8/8.13.8) with ESMTP id u1C38dMF027652 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=FAIL); Fri, 12 Feb 2016 03:08:39 GMT Received: from abhmp0004.oracle.com (abhmp0004.oracle.com [141.146.116.10]) by aserv0121.oracle.com (8.13.8/8.13.8) with ESMTP id u1C38dZ9030341; Fri, 12 Feb 2016 03:08:39 GMT Received: from localhost.localdomain.com (/209.6.196.81) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Thu, 11 Feb 2016 19:08:39 -0800 From: Konrad Rzeszutek Wilk To: ian.campbell@citrix.com, ian.jackson@eu.citrix.com, jbeulich@suse.com, xen-devel@lists.xenproject.org Date: Thu, 11 Feb 2016 22:08:27 -0500 Message-Id: <1455246507-5589-6-git-send-email-konrad.wilk@oracle.com> X-Mailer: git-send-email 2.4.3 In-Reply-To: <1455246507-5589-1-git-send-email-konrad.wilk@oracle.com> References: <1455246507-5589-1-git-send-email-konrad.wilk@oracle.com> X-Source-IP: aserv0021.oracle.com [141.146.126.233] Cc: Konrad Rzeszutek Wilk Subject: [Xen-devel] [PATCH v3 5/5] mkelf32: Close those file descriptors in the error paths. X-BeenThere: xen-devel@lists.xen.org X-Mailman-Version: 2.1.13 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_MED, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP While we are operating here we may as well fix some of the file descriptor leaks. Signed-off-by: Konrad Rzeszutek Wilk --- xen/arch/x86/boot/mkelf32.c | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/xen/arch/x86/boot/mkelf32.c b/xen/arch/x86/boot/mkelf32.c index 993a7ee..890ae6d 100644 --- a/xen/arch/x86/boot/mkelf32.c +++ b/xen/arch/x86/boot/mkelf32.c @@ -230,9 +230,9 @@ int main(int argc, char **argv) u64 final_exec_addr; u32 loadbase, dat_siz, mem_siz; char *inimage, *outimage; - int infd, outfd; + int infd, outfd = -1; char buffer[1024]; - int bytes, todo, i; + int bytes, todo, i, rc = 1; Elf32_Ehdr in32_ehdr; @@ -256,7 +256,7 @@ int main(int argc, char **argv) { fprintf(stderr, "Failed to open input image '%s': %d (%s).\n", inimage, errno, strerror(errno)); - return 1; + goto out; } do_read(infd, &in32_ehdr, sizeof(in32_ehdr)); @@ -264,7 +264,7 @@ int main(int argc, char **argv) (in32_ehdr.e_ident[EI_DATA] != ELFDATA2LSB) ) { fprintf(stderr, "Input image must be a little-endian Elf image.\n"); - return 1; + goto out; } big_endian = (*(u16 *)in32_ehdr.e_ident == ((ELFMAG0 << 8) | ELFMAG1)); @@ -273,7 +273,7 @@ int main(int argc, char **argv) if ( in32_ehdr.e_ident[EI_CLASS] != ELFCLASS64 ) { fprintf(stderr, "Bad program header class - we only do 64-bit!.\n"); - return 1; + goto out; } (void)lseek(infd, 0, SEEK_SET); do_read(infd, &in64_ehdr, sizeof(in64_ehdr)); @@ -283,14 +283,14 @@ int main(int argc, char **argv) { fprintf(stderr, "Bad program header size (%d != %d).\n", (int)in64_ehdr.e_phentsize, (int)sizeof(in64_phdr)); - return 1; + goto out; } if ( in64_ehdr.e_phnum != 1 ) { fprintf(stderr, "Expect precisly 1 program header; found %d.\n", (int)in64_ehdr.e_phnum); - return 1; + goto out; } (void)lseek(infd, in64_ehdr.e_phoff, SEEK_SET); @@ -327,7 +327,7 @@ int main(int argc, char **argv) { fprintf(stderr, "Failed to open output image '%s': %d (%s).\n", outimage, errno, strerror(errno)); - return 1; + goto out; } endianadjust_ehdr32(&out_ehdr); @@ -339,7 +339,7 @@ int main(int argc, char **argv) if ( (bytes = RAW_OFFSET - sizeof(out_ehdr) - sizeof(out_phdr)) < 0 ) { fprintf(stderr, "Header overflow.\n"); - return 1; + goto out; } do_write(outfd, buffer, bytes); @@ -358,10 +358,14 @@ int main(int argc, char **argv) do_write(outfd, out_shstrtab, sizeof(out_shstrtab)); do_write(outfd, buffer, 4-((sizeof(out_shstrtab)+dat_siz)&3)); - close(infd); - close(outfd); + rc = 0; +out: + if ( infd != -1 ) + close(infd); + if ( outfd != -1 ) + close(outfd); - return 0; + return rc; } /*