Skip to content

Commit f33a0b9

Browse files
Kaige Litsbogend
authored andcommitted
MIPS: tools: Fix resource leak in elf-entry.c
There is a file descriptor resource leak in elf-entry.c, fix this by adding fclose() before return and die. Signed-off-by: Kaige Li <likaige@loongson.cn> Signed-off-by: Thomas Bogendoerfer <tsbogend@alpha.franken.de>
1 parent b34a1a7 commit f33a0b9

1 file changed

Lines changed: 8 additions & 1 deletion

File tree

‎arch/mips/tools/elf-entry.c‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,14 @@ int main(int argc, const char *argv[])
5151
nread = fread(&hdr, 1, sizeof(hdr), file);
5252
if (nread != sizeof(hdr)) {
5353
perror("Unable to read input file");
54+
fclose(file);
5455
return EXIT_FAILURE;
5556
}
5657

57-
if (memcmp(hdr.ehdr32.e_ident, ELFMAG, SELFMAG))
58+
if (memcmp(hdr.ehdr32.e_ident, ELFMAG, SELFMAG)) {
59+
fclose(file);
5860
die("Input is not an ELF\n");
61+
}
5962

6063
switch (hdr.ehdr32.e_ident[EI_CLASS]) {
6164
case ELFCLASS32:
@@ -67,6 +70,7 @@ int main(int argc, const char *argv[])
6770
entry = be32toh(hdr.ehdr32.e_entry);
6871
break;
6972
default:
73+
fclose(file);
7074
die("Invalid ELF encoding\n");
7175
}
7276

@@ -83,14 +87,17 @@ int main(int argc, const char *argv[])
8387
entry = be64toh(hdr.ehdr64.e_entry);
8488
break;
8589
default:
90+
fclose(file);
8691
die("Invalid ELF encoding\n");
8792
}
8893
break;
8994

9095
default:
96+
fclose(file);
9197
die("Invalid ELF class\n");
9298
}
9399

94100
printf("0x%016" PRIx64 "\n", entry);
101+
fclose(file);
95102
return EXIT_SUCCESS;
96103
}

0 commit comments

Comments
 (0)