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
Mahmoud Mandour <ma.mandourr@gmail.com> writes:
> 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)
Given this is start-up code I think you could use g_new instead of
g_try_new. As it will abort on no memory you can avoid the early return
check bellow. Also is elf_phdata never persists beyond this function you
could use g_autofree (and use g_steal_pointer on the one case when it is
returned if you need it)
>
> 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);
>
That would allow you to get rid of a lot of free/g_frees
I would also split this patch, one for each function you convert.
> *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);
--
Alex Bennée
> > Given this is start-up code I think you could use g_new instead of > g_try_new. As it will abort on no memory you can avoid the early return > check bellow. Also is elf_phdata never persists beyond this function you > could use g_autofree (and use g_steal_pointer on the one case when it is > returned if you need it) > I would also split this patch, one for each function you convert. > Thank you for the valuable suggestions, these are obviously the better way to go. I will hopefully employ them. I'm now busy with my final exams. In a week or so, I will make the changes again and properly, split the patch, and then send it as a series regarding this file. Thanks Mahmoud Mandour
Mahmoud Mandour <ma.mandourr@gmail.com> writes: > Given this is start-up code I think you could use g_new instead of > g_try_new. As it will abort on no memory you can avoid the early return > check bellow. Also is elf_phdata never persists beyond this function you > could use g_autofree (and use g_steal_pointer on the one case when it is > returned if you need it) > > I would also split this patch, one for each function you convert. > > > Thank you for the valuable suggestions, these are obviously the better > way to go. I will hopefully employ them. > I'm now busy with my final exams. In a week or so, I will make the > changes again and properly, split the patch, and then send it as a > series regarding this file. We are also trying to improve the language in the coding style document if you see the thread: Subject: [RFC PATCH] docs/devel: expand style section of memory management Date: Mon, 15 Mar 2021 16:53:12 +0000 Message-Id: <20210315165312.22453-1-alex.bennee@linaro.org> apologies, I did intend to cc you as well but forgot in my haste to post. > > Thanks > Mahmoud Mandour -- Alex Bennée
© 2016 - 2026 Red Hat, Inc.