Skip to content

Commit 8d79de6

Browse files
Stefan WeilRiku Voipio
authored andcommitted
linux-user: Fix possible realloc memory leak
Extract from "man realloc": "If realloc() fails the original block is left untouched; it is not freed or moved." Fix a possible memory leak (reported by cppcheck). Cc: Riku Voipio <[email protected]> Signed-off-by: Stefan Weil <[email protected]> Signed-off-by: Riku Voipio <[email protected]>
1 parent 6672b0b commit 8d79de6

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

linux-user/elfload.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1481,7 +1481,7 @@ static void load_symbols(struct elfhdr *hdr, int fd, abi_ulong load_bias)
14811481
struct elf_shdr *shdr;
14821482
char *strings;
14831483
struct syminfo *s;
1484-
struct elf_sym *syms;
1484+
struct elf_sym *syms, *new_syms;
14851485

14861486
shnum = hdr->e_shnum;
14871487
i = shnum * sizeof(struct elf_shdr);
@@ -1550,12 +1550,14 @@ static void load_symbols(struct elfhdr *hdr, int fd, abi_ulong load_bias)
15501550
that we threw away. Whether or not this has any effect on the
15511551
memory allocation depends on the malloc implementation and how
15521552
many symbols we managed to discard. */
1553-
syms = realloc(syms, nsyms * sizeof(*syms));
1554-
if (syms == NULL) {
1553+
new_syms = realloc(syms, nsyms * sizeof(*syms));
1554+
if (new_syms == NULL) {
15551555
free(s);
1556+
free(syms);
15561557
free(strings);
15571558
return;
15581559
}
1560+
syms = new_syms;
15591561

15601562
qsort(syms, nsyms, sizeof(*syms), symcmp);
15611563

0 commit comments

Comments
 (0)