[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH 1/9] bsd-user/elfload.c: Replaced calls to malloc/free with GLib
From: |
Mahmoud Mandour |
Subject: |
[PATCH 1/9] bsd-user/elfload.c: Replaced calls to malloc/free with GLib variants |
Date: |
Sat, 13 Mar 2021 18:36:45 +0200 |
Replaced the calls to malloc(), realloc(), and free() to their
equivalents in GLib's allocation functions in various places.
Signed-off-by: Mahmoud Mandour <ma.mandourr@gmail.com>
---
bsd-user/elfload.c | 74 +++++++++++++++++++++++-----------------------
1 file changed, 37 insertions(+), 37 deletions(-)
diff --git a/bsd-user/elfload.c b/bsd-user/elfload.c
index 5f4d824d78..7b0793693b 100644
--- a/bsd-user/elfload.c
+++ b/bsd-user/elfload.c
@@ -867,8 +867,7 @@ 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);
+ elf_phdata = g_try_new(struct elf_phdr, interp_elf_ex->ephnum)
if (!elf_phdata)
return ~((abi_ulong)0UL);
@@ -878,7 +877,7 @@ static abi_ulong load_elf_interp(struct elfhdr *
interp_elf_ex,
* 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);
}
@@ -891,7 +890,7 @@ static abi_ulong load_elf_interp(struct elfhdr *
interp_elf_ex,
if (retval < 0) {
perror("load_elf_interp");
exit(-1);
- free (elf_phdata);
+ g_free(elf_phdata);
return retval;
}
#ifdef BSWAP_NEEDED
@@ -940,7 +939,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 +982,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 +1063,24 @@ 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);
+ s = g_try_malloc(sizeof(*s));
+ syms = g_try_malloc(symtab.sh_size);
if (!syms) {
- free(s);
+ g_free(s);
return;
}
- s->disas_strtab = strings = malloc(strtab.sh_size);
+ s->disas_strtab = strings = g_malloc(strtab.sh_size);
if (!s->disas_strtab) {
- free(s);
- free(syms);
+ g_free(s);
+ g_free(syms);
return;
}
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,11 +1112,11 @@ 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));
+ new_syms = g_try_realloc(syms, nsyms * sizeof(*syms));
if (new_syms == NULL) {
- free(s);
- free(syms);
- free(strings);
+ g_free(s);
+ g_free(syms);
+ g_free(strings);
return;
}
syms = new_syms;
@@ -1126,9 +1125,9 @@ static void load_symbols(struct elfhdr *hdr, int fd)
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,7 +1189,8 @@ 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);
+ elf_phdata =
+ (struct elf_phdr *)g_try_malloc(elf_ex.e_phentsizei * elf_ex.e_phnum);
if (elf_phdata == NULL) {
return -ENOMEM;
}
@@ -1204,7 +1204,7 @@ int load_elf_binary(struct linux_binprm * bprm, struct
target_pt_regs * regs,
if (retval < 0) {
perror("load_elf_binary");
exit(-1);
- free (elf_phdata);
+ g_free(elf_phdata);
return -errno;
}
@@ -1231,8 +1231,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,10 +1242,10 @@ 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);
+ elf_interpreter = (char *)g_try_malloc(elf_ppnt->p_filesz);
if (elf_interpreter == NULL) {
- free (elf_phdata);
+ g_free(elf_phdata);
close(bprm->fd);
return -ENOMEM;
}
@@ -1298,8 +1298,8 @@ 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);
return retval;
}
@@ -1323,8 +1323,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 +1346,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 +1486,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
- [PATCH 0/9] Replacing malloc and the like with GLib's variants, Mahmoud Mandour, 2021/03/13
- [PATCH 1/9] bsd-user/elfload.c: Replaced calls to malloc/free with GLib variants,
Mahmoud Mandour <=
- [PATCH 2/9] hw/audio/fmopl.c: Fixing some style errors., Mahmoud Mandour, 2021/03/13
- [PATCH 8/9] tools/virtiofsd/fuse_opt.c: Replaced a malloc with GLib's g_try_malloc, Mahmoud Mandour, 2021/03/13
- [PATCH 6/9] util/compatfd.c: Replaced a malloc with GLib's g_try_malloc, Mahmoud Mandour, 2021/03/13
- [PATCH 5/9] target/xtensa: Replaced malloc/free with GLib's variants., Mahmoud Mandour, 2021/03/13
- [PATCH 3/9] hw/audio/fmopl.c: Replaced calls to malloc with GLib's variants, Mahmoud Mandour, 2021/03/13
- [PATCH 7/9] tools/virtiofsd/buffer.c: replaced a calloc call with GLib's g_try_new0, Mahmoud Mandour, 2021/03/13
- [PATCH 4/9] pc-bios/s390-ccw/netmain.c: Changed a malloc/free to GLib's variants, Mahmoud Mandour, 2021/03/13
- [PATCH 9/9] tools/virtiofsd: Replacing malloc-like calls with GLib's variants, Mahmoud Mandour, 2021/03/13