[PATCH] disas: converts malloc to g_malloc0

Christian S. Lima posted 1 patch 3 weeks, 5 days ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/20260630005249.143976-1-christianslima@proton.me
Maintainers: Laurent Vivier <laurent@vivier.eu>
disas/m68k.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
[PATCH] disas: converts malloc to g_malloc0
Posted by Christian S. Lima 3 weeks, 5 days ago
Following the qemu coding style change malloc to g_malloc, the
advantages are that g_malloc will exit if some failure occur and
initialize all memory with zeros.

Signed-off-by: Christian S. Lima <christianslima@proton.me>
---
 disas/m68k.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/disas/m68k.c b/disas/m68k.c
index 800b4145ac..e629f3c365 100644
--- a/disas/m68k.c
+++ b/disas/m68k.c
@@ -1887,8 +1887,8 @@ print_insn_m68k (bfd_vma memaddr, disassemble_info *info)
 
       /* Then create a sorted table of pointers
 	 that point into the unsorted table.  */
-      opc_pointer[0] = malloc (sizeof (struct m68k_opcode *)
-                               * m68k_numopcodes);
+      opc_pointer[0] = g_malloc0 (sizeof (struct m68k_opcode)
+                                  * m68k_numopcodes);
       opcodes[0] = opc_pointer[0];
 
       for (i = 1; i < 16; i++)
-- 
2.53.0
Re: [PATCH] disas: converts malloc to g_malloc0
Posted by Markus Armbruster 3 weeks, 5 days ago
"Christian S. Lima" <christianslima@proton.me> writes:

> Following the qemu coding style change malloc to g_malloc, the
> advantages are that g_malloc will exit if some failure occur and
> initialize all memory with zeros.

Any particular reason to initialize, or is it "just in case"?

> Signed-off-by: Christian S. Lima <christianslima@proton.me>
> ---
>  disas/m68k.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/disas/m68k.c b/disas/m68k.c
> index 800b4145ac..e629f3c365 100644
> --- a/disas/m68k.c
> +++ b/disas/m68k.c
> @@ -1887,8 +1887,8 @@ print_insn_m68k (bfd_vma memaddr, disassemble_info *info)
>  
>        /* Then create a sorted table of pointers
>  	 that point into the unsorted table.  */
> -      opc_pointer[0] = malloc (sizeof (struct m68k_opcode *)
> -                               * m68k_numopcodes);
> +      opc_pointer[0] = g_malloc0 (sizeof (struct m68k_opcode)
> +                                  * m68k_numopcodes);
>        opcodes[0] = opc_pointer[0];
>  
>        for (i = 1; i < 16; i++)

g_new0(struct m68k_opcode, m68k_numopcodes) would be more obviously
safe, because it checks the multiplication for for overflow.
Re: [PATCH] disas: converts malloc to g_malloc0
Posted by Christian 3 weeks, 5 days ago
Hi, Markus!

> Any particular reason to initialize, or is it "just in case"?

It's a just in case, the next lines initialize the values. 

> g_new0(struct m68k_opcode, m68k_numopcodes) would be more obviously
> safe, because it checks the multiplication for for overflow.

When I tried g_new0, but the first argument is a pointer to `struct m68k_opcode`, but it gives me an error, so I just used g_malloc0 instead. If I pass just the struct as the first argument gives me the same result as using the g_malloc0?

Thanks,
Christian
Re: [PATCH] disas: converts malloc to g_malloc0
Posted by Markus Armbruster 3 weeks, 5 days ago
Christian <christianslima@proton.me> writes:

> Hi, Markus!
>
>> Any particular reason to initialize, or is it "just in case"?
>
> It's a just in case, the next lines initialize the values. 

Suggest to mention that in the commit message, just to be perfectly
clear.

>> g_new0(struct m68k_opcode, m68k_numopcodes) would be more obviously
>> safe, because it checks the multiplication for for overflow.
>
> When I tried g_new0, but the first argument is a pointer to `struct m68k_opcode`, but it gives me an error, so I just used g_malloc0 instead. If I pass just the struct as the first argument gives me the same result as using the g_malloc0?

g_new0(T, N) allocates an array T[N].  The macro's return value is a T
*.  So, g_new0(struct m68k_opcode *, m68k_numopcodes) returns struct
m68k_opcode **, which fails type checking.  Good, because it's indeed
wrong: you want an array of T, not an array of T *.

Details at <https://docs.gtk.org/glib/func.new0.html>.

Questions?
Re: [PATCH] disas: converts malloc to g_malloc0
Posted by Christian 3 weeks, 4 days ago
> g_new0(T, N) allocates an array T[N].  The macro's return value is a T
> *.  So, g_new0(struct m68k_opcode *, m68k_numopcodes) returns struct
> m68k_opcode **, which fails type checking.  Good, because it's indeed
> wrong: you want an array of T, not an array of T *.
> 
> Details at <https://docs.gtk.org/glib/func.new0.html>.
> 
> Questions?

Thanks for your explanation, now I understand what's wrong. I'll submit a v2 later. :)