From: Elena Afanasova <eafanasova@gmail.com>
To: qemu-devel@nongnu.org, armbru@redhat.com, thuth@redhat.com,
kamil@netbsd.org, brad@comstyle.com, emaste@freebsd.org,
lwhsu@freebsd.org
Cc: qemu-trivial@nongnu.org
Subject: [PATCH v2] bsd-user/elfload: use g_new/g_malloc
Date: Wed, 07 Oct 2020 14:54:52 -0700 [thread overview]
Message-ID: <f21781e85b10bdce0a3ecc87304e8214096140d8.camel@gmail.com> (raw)
Signed-off-by: Elena Afanasova <eafanasova@gmail.com>
---
bsd-user/elfload.c | 88 ++++++++++++++++------------------------------
1 file changed, 30 insertions(+), 58 deletions(-)
diff --git a/bsd-user/elfload.c b/bsd-user/elfload.c
index 32378af7b2..f1be01c410 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,8 +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
@@ -940,7 +936,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 +979,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 +1060,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_malloc(sizeof(*s));
+ syms = g_malloc(symtab.sh_size);
+ s->disas_strtab = strings = g_malloc(strtab.sh_size);
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 +1100,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;
}
s->disas_num_syms = nsyms;
@@ -1190,10 +1171,7 @@ int load_elf_binary(struct linux_binprm * bprm, struct target_pt_regs * regs,
}
/* Now read in all of the header information */
- elf_phdata = (struct elf_phdr *)malloc(elf_ex.e_phentsize*elf_ex.e_phnum);
- if (elf_phdata == NULL) {
- return -ENOMEM;
- }
+ elf_phdata = g_malloc(elf_ex.e_phentsize * elf_ex.e_phnum);
retval = lseek(bprm->fd, elf_ex.e_phoff, SEEK_SET);
if(retval > 0) {
@@ -1203,8 +1181,8 @@ int load_elf_binary(struct linux_binprm * bprm, struct target_pt_regs * regs,
if (retval < 0) {
perror("load_elf_binary");
+ g_free(elf_phdata);
exit(-1);
- free (elf_phdata);
return -errno;
}
@@ -1231,8 +1209,8 @@ int load_elf_binary(struct linux_binprm * bprm, struct target_pt_regs * regs,
if (elf_ppnt->p_type == PT_INTERP) {
if ( elf_interpreter != NULL )
{
- free (elf_phdata);
- free(elf_interpreter);
+ g_free(elf_phdata);
+ g_free(elf_interpreter);
close(bprm->fd);
return -EINVAL;
}
@@ -1242,13 +1220,7 @@ int load_elf_binary(struct linux_binprm * bprm, struct target_pt_regs * regs,
* is an a.out format binary
*/
- elf_interpreter = (char *)malloc(elf_ppnt->p_filesz);
-
- if (elf_interpreter == NULL) {
- free (elf_phdata);
- close(bprm->fd);
- return -ENOMEM;
- }
+ elf_interpreter = g_malloc(elf_ppnt->p_filesz);
retval = lseek(bprm->fd, elf_ppnt->p_offset, SEEK_SET);
if(retval >= 0) {
@@ -1297,10 +1269,10 @@ int load_elf_binary(struct linux_binprm * bprm, struct target_pt_regs * regs,
}
if (retval < 0) {
perror("load_elf_binary3");
- exit(-1);
- free (elf_phdata);
- free(elf_interpreter);
+ g_free(elf_phdata);
+ g_free(elf_interpreter);
close(bprm->fd);
+ exit(-1);
return retval;
}
}
@@ -1323,8 +1295,8 @@ int load_elf_binary(struct linux_binprm * bprm, struct target_pt_regs * regs,
}
if (!interpreter_type) {
- free(elf_interpreter);
- free(elf_phdata);
+ g_free(elf_interpreter);
+ g_free(elf_phdata);
close(bprm->fd);
return -ELIBBAD;
}
@@ -1346,8 +1318,8 @@ int load_elf_binary(struct linux_binprm * bprm, struct target_pt_regs * regs,
}
}
if (!bprm->p) {
- free(elf_interpreter);
- free (elf_phdata);
+ g_free(elf_interpreter);
+ g_free(elf_phdata);
close(bprm->fd);
return -E2BIG;
}
@@ -1486,17 +1458,17 @@ int load_elf_binary(struct linux_binprm * bprm, struct target_pt_regs * regs,
reloc_func_desc = interp_load_addr;
close(interpreter_fd);
- free(elf_interpreter);
+ g_free(elf_interpreter);
if (elf_entry == ~((abi_ulong)0UL)) {
printf("Unable to load interpreter\n");
- free(elf_phdata);
+ g_free(elf_phdata);
exit(-1);
return 0;
}
}
- free(elf_phdata);
+ g_free(elf_phdata);
if (qemu_log_enabled())
load_symbols(&elf_ex, bprm->fd);
--
2.25.1
reply other threads:[~2020-10-07 21:57 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=f21781e85b10bdce0a3ecc87304e8214096140d8.camel@gmail.com \
--to=eafanasova@gmail.com \
--cc=armbru@redhat.com \
--cc=brad@comstyle.com \
--cc=emaste@freebsd.org \
--cc=kamil@netbsd.org \
--cc=lwhsu@freebsd.org \
--cc=qemu-devel@nongnu.org \
--cc=qemu-trivial@nongnu.org \
--cc=thuth@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).