On 01/10/2020 14.38, Elena Afanasova wrote: > Signed-off-by: Elena Afanasova <eafanas...@gmail.com> > --- > bsd-user/elfload.c | 92 +++++++++++++++------------------------------- > 1 file changed, 30 insertions(+), 62 deletions(-) > > diff --git a/bsd-user/elfload.c b/bsd-user/elfload.c > index 32378af7b2..e10ca54eb7 100644 > --- a/bsd-user/elfload.c > +++ b/bsd-user/elfload.c > @@ -867,18 +867,14 @@ static abi_ulong load_elf_interp(struct elfhdr * > interp_elf_ex, > if (sizeof(struct elf_phdr) * interp_elf_ex->e_phnum > > TARGET_PAGE_SIZE) > return ~(abi_ulong)0UL; > > - elf_phdata = (struct elf_phdr *) > - malloc(sizeof(struct elf_phdr) * interp_elf_ex->e_phnum); > - > - if (!elf_phdata) > - return ~((abi_ulong)0UL); > + elf_phdata = g_new(struct elf_phdr, interp_elf_ex->e_phnum); > > /* > * If the size of this structure has changed, then punt, since > * we will be doing the wrong thing. > */ > if (interp_elf_ex->e_phentsize != sizeof(struct elf_phdr)) { > - free(elf_phdata); > + g_free(elf_phdata); > return ~((abi_ulong)0UL); > } > > @@ -890,9 +886,8 @@ static abi_ulong load_elf_interp(struct elfhdr * > interp_elf_ex, > } > if (retval < 0) { > perror("load_elf_interp"); > + g_free(elf_phdata); > exit(-1); > - free (elf_phdata); > - return retval; > } > #ifdef BSWAP_NEEDED > eppnt = elf_phdata; > @@ -940,7 +935,7 @@ static abi_ulong load_elf_interp(struct elfhdr * > interp_elf_ex, > if (error == -1) { > /* Real error */ > close(interpreter_fd); > - free(elf_phdata); > + g_free(elf_phdata); > return ~((abi_ulong)0UL); > } > > @@ -983,7 +978,7 @@ static abi_ulong load_elf_interp(struct elfhdr * > interp_elf_ex, > PROT_READ|PROT_WRITE|PROT_EXEC, > MAP_FIXED|MAP_PRIVATE|MAP_ANON, -1, 0); > } > - free(elf_phdata); > + g_free(elf_phdata); > > *interp_load_addr = load_addr; > return ((abi_ulong) interp_elf_ex->e_entry) + load_addr; > @@ -1064,24 +1059,15 @@ static void load_symbols(struct elfhdr *hdr, int fd) > > found: > /* Now know where the strtab and symtab are. Snarf them. */ > - s = malloc(sizeof(*s)); > - syms = malloc(symtab.sh_size); > - if (!syms) { > - free(s); > - return; > - } > - s->disas_strtab = strings = malloc(strtab.sh_size); > - if (!s->disas_strtab) { > - free(s); > - free(syms); > - return; > - } > + s = g_new(struct syminfo, 1); > + syms = g_new(symtab.sh_size, 1); > + s->disas_strtab = strings = g_new(strtab.sh_size, 1); > > lseek(fd, symtab.sh_offset, SEEK_SET); > if (read(fd, syms, symtab.sh_size) != symtab.sh_size) { > - free(s); > - free(syms); > - free(strings); > + g_free(s); > + g_free(syms); > + g_free(strings); > return; > } > > @@ -1113,22 +1099,16 @@ static void load_symbols(struct elfhdr *hdr, int fd) > that we threw away. Whether or not this has any effect on the > memory allocation depends on the malloc implementation and how > many symbols we managed to discard. */ > - new_syms = realloc(syms, nsyms * sizeof(*syms)); > - if (new_syms == NULL) { > - free(s); > - free(syms); > - free(strings); > - return; > - } > + new_syms = g_realloc(syms, nsyms * sizeof(*syms)); > syms = new_syms; > > qsort(syms, nsyms, sizeof(*syms), symcmp); > > lseek(fd, strtab.sh_offset, SEEK_SET); > if (read(fd, strings, strtab.sh_size) != strtab.sh_size) { > - free(s); > - free(syms); > - free(strings); > + g_free(s); > + g_free(syms); > + g_free(strings); > return; > } [...]
Hi! That clean-up sounds like a very good idea ... but I think this would be a perfect place to use g_autofree in the declaration of the variables, then you can get rid of the free() calls completely! Thomas