QEMU-Devel Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] disas: converts malloc to g_new0
@ 2026-06-30 22:32 Christian S. Lima
  2026-07-01  5:16 ` Markus Armbruster
  0 siblings, 1 reply; 3+ messages in thread
From: Christian S. Lima @ 2026-06-30 22:32 UTC (permalink / raw)
  To: qemu-devel, Laurent Vivier

Following the qemu coding style change from malloc to g_new0, the
advantages are that g_new0 catch multiplication overflowing size_t,
allows for better detection of type errors because it returns the type
itself and initialize memory with zeros to detect if something go wrong.

Signed-off-by: Christian S. Lima <christianslima@proton.me>
---
Changes in v2:
    - Change from g_malloc0 to g_new0
---
 disas/m68k.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/disas/m68k.c b/disas/m68k.c
index 800b4145ac..c3571783b0 100644
--- a/disas/m68k.c
+++ b/disas/m68k.c
@@ -1887,8 +1887,7 @@ 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_new0(const 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] 3+ messages in thread

* Re: [PATCH v2] disas: converts malloc to g_new0
  2026-06-30 22:32 [PATCH v2] disas: converts malloc to g_new0 Christian S. Lima
@ 2026-07-01  5:16 ` Markus Armbruster
  2026-07-01 10:22   ` Peter Maydell
  0 siblings, 1 reply; 3+ messages in thread
From: Markus Armbruster @ 2026-07-01  5:16 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 from malloc to g_new0, the
> advantages are that g_new0 catch multiplication overflowing size_t,
> allows for better detection of type errors because it returns the type
> itself and initialize memory with zeros to detect if something go wrong.

In general, just-in-case zero-initialization won't detect anything, it
just converts unpredictable bad behavior to predictable bad behavior.

In this case, it does exactly nothing, as we'll see below.

>
> Signed-off-by: Christian S. Lima <christianslima@proton.me>
> ---
> Changes in v2:
>     - Change from g_malloc0 to g_new0
> ---
>  disas/m68k.c | 3 +--
>  1 file changed, 1 insertion(+), 2 deletions(-)
>
> diff --git a/disas/m68k.c b/disas/m68k.c
> index 800b4145ac..c3571783b0 100644
> --- a/disas/m68k.c
> +++ b/disas/m68k.c
> @@ -1887,8 +1887,7 @@ 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_new0(const struct m68k_opcode *, m68k_numopcodes);
>        opcodes[0] = opc_pointer[0];
>  
>        for (i = 1; i < 16; i++)
           {
             opc_pointer[i] = opc_pointer[i - 1] + numopcodes[i - 1];
             opcodes[i] = opc_pointer[i];
           }

This obviously overwrites every single bit in opc_pointer[], i.e. the
switch from allocating uninitialized to zero-initialzed memory is a
complete waste.  I'd use g_new().  Up to the maintainer.

Either way,
Reviewed-by: Markus Armbruster <armbru@redhat.com>



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

* Re: [PATCH v2] disas: converts malloc to g_new0
  2026-07-01  5:16 ` Markus Armbruster
@ 2026-07-01 10:22   ` Peter Maydell
  0 siblings, 0 replies; 3+ messages in thread
From: Peter Maydell @ 2026-07-01 10:22 UTC (permalink / raw)
  To: Markus Armbruster; +Cc: Christian S. Lima, qemu-devel, Laurent Vivier

On Wed, 1 Jul 2026 at 06:17, Markus Armbruster <armbru@redhat.com> wrote:
> Up to the maintainer.

I'm not the maintainer here but I'm kind of coming to the
opinion that where capstone supports an architecture (as it
does for at least alpha, m68k, xtensa, sparc) we should rip
out our local disassembler entirely. This old "borrowed from
ancient binutils" disassembler code is not worth the effort
we end up spending on making tweaks to it prompted by
static analyzers and other tree-wide stuff.

-- PMM


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

end of thread, other threads:[~2026-07-01 10:22 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-06-30 22:32 [PATCH v2] disas: converts malloc to g_new0 Christian S. Lima
2026-07-01  5:16 ` Markus Armbruster
2026-07-01 10:22   ` Peter Maydell

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox