From mboxrd@z Thu Jan 1 00:00:00 1970 From: Konrad Rzeszutek Wilk Subject: [PATCH v3 5/5] mkelf32: Close those file descriptors in the error paths. Date: Thu, 11 Feb 2016 22:08:27 -0500 Message-ID: <1455246507-5589-6-git-send-email-konrad.wilk@oracle.com> References: <1455246507-5589-1-git-send-email-konrad.wilk@oracle.com> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Return-path: 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 In-Reply-To: <1455246507-5589-1-git-send-email-konrad.wilk@oracle.com> List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org To: ian.campbell@citrix.com, ian.jackson@eu.citrix.com, jbeulich@suse.com, xen-devel@lists.xenproject.org Cc: Konrad Rzeszutek Wilk List-Id: xen-devel@lists.xenproject.org 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; } /* -- 2.4.3