From: Markus Armbruster <armbru@redhat.com>
To: "Christian S. Lima" <christianslima@proton.me>
Cc: qemu-devel@nongnu.org, Laurent Vivier <laurent@vivier.eu>
Subject: Re: [PATCH v2] disas: converts malloc to g_new0
Date: Wed, 01 Jul 2026 07:16:54 +0200 [thread overview]
Message-ID: <87echnwapl.fsf@pond.sub.org> (raw)
In-Reply-To: <20260630223159.68737-1-christianslima@proton.me> (Christian S. Lima's message of "Tue, 30 Jun 2026 22:32:04 +0000")
"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>
next prev parent reply other threads:[~2026-07-01 5:17 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-06-30 22:32 [PATCH v2] disas: converts malloc to g_new0 Christian S. Lima
2026-07-01 5:16 ` Markus Armbruster [this message]
2026-07-01 10:22 ` Peter Maydell
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87echnwapl.fsf@pond.sub.org \
--to=armbru@redhat.com \
--cc=christianslima@proton.me \
--cc=laurent@vivier.eu \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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.