[PATCH v2] disas: converts malloc to g_new0

Christian S. Lima posted 1 patch 3 weeks, 4 days ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/20260630223159.68737-1-christianslima@proton.me
Maintainers: Laurent Vivier <laurent@vivier.eu>
disas/m68k.c | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
[PATCH v2] disas: converts malloc to g_new0
Posted by Christian S. Lima 3 weeks, 4 days ago
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
Re: [PATCH v2] disas: converts malloc to g_new0
Posted by Markus Armbruster 3 weeks, 4 days ago
"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>
Re: [PATCH v2] disas: converts malloc to g_new0
Posted by Peter Maydell 3 weeks, 3 days ago
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