Skip to content

Commit

Permalink
Merge pull request #159 from flaviojs/fix-resource-leaks-ppc32_load_e…
Browse files Browse the repository at this point in the history
…lf_image

Fix resource leaks in ppc32_load_elf_image
  • Loading branch information
grossmj authored Mar 11, 2024
2 parents 56ff72d + 1ca9aef commit a80b0d0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
8 changes: 8 additions & 0 deletions stable/ppc32.c
Original file line number Diff line number Diff line change
Expand Up @@ -564,17 +564,21 @@ int ppc32_load_elf_image(cpu_ppc_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);
}

Expand All @@ -583,6 +587,8 @@ int ppc32_load_elf_image(cpu_ppc_t *cpu,char *filename,int skip_load,

if (!bfd) {
perror("load_elf_image: fdopen");
elf_end(img_elf);
close(fd);
return(-1);
}

Expand Down Expand Up @@ -612,6 +618,8 @@ int ppc32_load_elf_image(cpu_ppc_t *cpu,char *filename,int skip_load,
if (!haddr) {
fprintf(stderr,"load_elf_image: invalid load address 0x%x\n",
vaddr);
elf_end(img_elf);
fclose(bfd);
return(-1);
}

Expand Down
8 changes: 8 additions & 0 deletions unstable/ppc32.c
Original file line number Diff line number Diff line change
Expand Up @@ -561,17 +561,21 @@ int ppc32_load_elf_image(cpu_ppc_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);
}

Expand All @@ -580,6 +584,8 @@ int ppc32_load_elf_image(cpu_ppc_t *cpu,char *filename,int skip_load,

if (!bfd) {
perror("load_elf_image: fdopen");
elf_end(img_elf);
close(fd);
return(-1);
}

Expand Down Expand Up @@ -609,6 +615,8 @@ int ppc32_load_elf_image(cpu_ppc_t *cpu,char *filename,int skip_load,
if (!haddr) {
fprintf(stderr,"load_elf_image: invalid load address 0x%x\n",
vaddr);
elf_end(img_elf);
fclose(bfd);
return(-1);
}

Expand Down

0 comments on commit a80b0d0

Please sign in to comment.