From 9f29ebbcca4a5abfd69d1c3b12c7513fa4f0f962 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fl=C3=A1vio=20J=2E=20Saraiva?= Date: Sat, 9 Mar 2024 20:29:06 +0000 Subject: [PATCH] Fix resource leaks in mips64_load_elf_image --- stable/mips64.c | 6 ++++++ unstable/mips64.c | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/stable/mips64.c b/stable/mips64.c index 6ea30f366..ad194ecb7 100644 --- a/stable/mips64.c +++ b/stable/mips64.c @@ -938,17 +938,21 @@ int mips64_load_elf_image(cpu_mips_t *cpu,char *filename,int skip_load, if (elf_version(EV_CURRENT) == EV_NONE) { fprintf(stderr,"load_elf_image: library out of date\n"); + close(fd); return(-1); } if (!(img_elf = elf_begin(fd,ELF_C_READ,NULL))) { fprintf(stderr,"load_elf_image: elf_begin: %s\n", elf_errmsg(elf_errno())); + close(fd); return(-1); } if (!(ehdr = elf32_getehdr(img_elf))) { fprintf(stderr,"load_elf_image: invalid ELF file\n"); + elf_end(img_elf); + close(fd); return(-1); } @@ -957,6 +961,8 @@ int mips64_load_elf_image(cpu_mips_t *cpu,char *filename,int skip_load, if (!bfd) { perror("load_elf_image: fdopen"); + elf_end(img_elf); + close(fd); return(-1); } diff --git a/unstable/mips64.c b/unstable/mips64.c index e301abcd6..99ff9293b 100644 --- a/unstable/mips64.c +++ b/unstable/mips64.c @@ -1021,17 +1021,21 @@ int mips64_load_elf_image(cpu_mips_t *cpu,char *filename,int skip_load, if (elf_version(EV_CURRENT) == EV_NONE) { fprintf(stderr,"load_elf_image: library out of date\n"); + close(fd); return(-1); } if (!(img_elf = elf_begin(fd,ELF_C_READ,NULL))) { fprintf(stderr,"load_elf_image: elf_begin: %s\n", elf_errmsg(elf_errno())); + close(fd); return(-1); } if (!(ehdr = elf32_getehdr(img_elf))) { fprintf(stderr,"load_elf_image: invalid ELF file\n"); + elf_end(img_elf); + close(fd); return(-1); } @@ -1040,6 +1044,8 @@ int mips64_load_elf_image(cpu_mips_t *cpu,char *filename,int skip_load, if (!bfd) { perror("load_elf_image: fdopen"); + elf_end(img_elf); + close(fd); return(-1); }