All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] disas: converts malloc to g_malloc0
@ 2026-06-30  0:52 Christian S. Lima
  2026-06-30  4:55 ` Markus Armbruster
  0 siblings, 1 reply; 5+ messages in thread
From: Christian S. Lima @ 2026-06-30  0:52 UTC (permalink / raw)
  To: qemu-devel, Laurent Vivier

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




^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] disas: converts malloc to g_malloc0
  2026-06-30  0:52 [PATCH] disas: converts malloc to g_malloc0 Christian S. Lima
@ 2026-06-30  4:55 ` Markus Armbruster
  2026-06-30  6:02   ` Christian
  0 siblings, 1 reply; 5+ messages in thread
From: Markus Armbruster @ 2026-06-30  4:55 UTC (permalink / raw)
  To: Christian S. Lima; +Cc: qemu-devel, Laurent Vivier

"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.



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] disas: converts malloc to g_malloc0
  2026-06-30  4:55 ` Markus Armbruster
@ 2026-06-30  6:02   ` Christian
  2026-06-30  6:53     ` Markus Armbruster
  0 siblings, 1 reply; 5+ messages in thread
From: Christian @ 2026-06-30  6:02 UTC (permalink / raw)
  To: Markus Armbruster; +Cc: qemu-devel, Laurent Vivier

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


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] disas: converts malloc to g_malloc0
  2026-06-30  6:02   ` Christian
@ 2026-06-30  6:53     ` Markus Armbruster
  2026-06-30 21:41       ` Christian
  0 siblings, 1 reply; 5+ messages in thread
From: Markus Armbruster @ 2026-06-30  6:53 UTC (permalink / raw)
  To: Christian; +Cc: qemu-devel, Laurent Vivier

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?



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] disas: converts malloc to g_malloc0
  2026-06-30  6:53     ` Markus Armbruster
@ 2026-06-30 21:41       ` Christian
  0 siblings, 0 replies; 5+ messages in thread
From: Christian @ 2026-06-30 21:41 UTC (permalink / raw)
  To: Markus Armbruster; +Cc: qemu-devel, Laurent Vivier

> 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. :)


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-06-30 21:42 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-06-30  0:52 [PATCH] disas: converts malloc to g_malloc0 Christian S. Lima
2026-06-30  4:55 ` Markus Armbruster
2026-06-30  6:02   ` Christian
2026-06-30  6:53     ` Markus Armbruster
2026-06-30 21:41       ` Christian

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.