qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Eric Blake <eblake@redhat.com>
To: Markus Armbruster <armbru@redhat.com>, qemu-devel@nongnu.org
Cc: peter.maydell@linaro.org, mdroth@linux.vnet.ibm.com,
	aliguori@amazon.com, agraf@suse.de
Subject: Re: [Qemu-devel] [PATCH 2/2] Use g_new() & friends where that makes obvious sense
Date: Fri, 31 Jan 2014 10:10:17 -0700	[thread overview]
Message-ID: <52EBD8F9.4080800@redhat.com> (raw)
In-Reply-To: <1391183604-21621-3-git-send-email-armbru@redhat.com>

[-- Attachment #1: Type: text/plain, Size: 2712 bytes --]

On 01/31/2014 08:53 AM, Markus Armbruster wrote:
> g_new(T, n) is safer than g_malloc(sizeof(T) * n) for two reasons.
> One, it catches multiplication overflowing size_t.  Two, it returns
> T * rather than void *, which lets the compiler catch more type
> errors.
> 
> Patch created with the following Coccinelle patch, with two hunks
> dropped from the result:
> 

Looks like a reasonable formula for replacement.

> 
> Signed-off-by: Markus Armbruster <armbru@redhat.com>
> ---

>  186 files changed, 375 insertions(+), 414 deletions(-)

Is there any easy way to enforce that this style does not creep back
into the code base?  In other words, can you do a followup patch to
checkpatch.pl that flags use of g_malloc[0]?


> @@ -658,7 +658,7 @@ int qcow2_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab)
>          return s->nb_snapshots;
>      }
>  
> -    sn_tab = g_malloc0(s->nb_snapshots * sizeof(QEMUSnapshotInfo));
> +    sn_tab = g_new0(QEMUSnapshotInfo, s->nb_snapshots);
>      for(i = 0; i < s->nb_snapshots; i++) {

Should checkpatch.pl care about the spacing after 'for'?  Then again,
it's unrelated to your conversion, so omitting a whitespace cleanup from
this patch doesn't hurt.

> +++ b/hw/char/virtio-serial-bus.c
> @@ -921,10 +921,8 @@ static void virtio_serial_device_realize(DeviceState *dev, Error **errp)
>      QTAILQ_INIT(&vser->ports);
>  
>      vser->bus.max_nr_ports = vser->serial.max_virtserial_ports;
> -    vser->ivqs = g_malloc(vser->serial.max_virtserial_ports
> -                          * sizeof(VirtQueue *));
> -    vser->ovqs = g_malloc(vser->serial.max_virtserial_ports
> -                          * sizeof(VirtQueue *));
> +    vser->ivqs = g_new(VirtQueue *, vser->serial.max_virtserial_ports);
> +    vser->ovqs = g_new(VirtQueue *, vser->serial.max_virtserial_ports);

I'm impressed at what Coccinelle can rewrite!

> +++ b/ui/keymaps.c
> @@ -107,7 +107,7 @@ static kbd_layout_t *parse_keyboard_layout(const name2keysym_t *table,
>      }
>  
>      if (!k)
> -	k = g_malloc0(sizeof(kbd_layout_t));
> +        k = g_new0(kbd_layout_t, 1);
>  
>      for(;;) {

Fixing tab damage while you are at it - nice.  And just noticed this is
another instance of no space after 'for', but not worth fixing in this
patch if checkpatch.pl is happy.

I'm trusting coccinelle, and only glanced through random spots of the
patch; but where I looked, I didn't find any problems in the conversion.

I don't know if that means we should add:
Reviewed-by: Eric Blake <eblake@redhat.com>

-- 
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 604 bytes --]

  reply	other threads:[~2014-01-31 17:10 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-01-31 15:53 [Qemu-devel] [PATCH 0/2] Use g_new() & friends where that makes obvious sense Markus Armbruster
2014-01-31 15:53 ` [Qemu-devel] [PATCH 1/2] qga: Fix memory allocation pasto Markus Armbruster
2014-01-31 16:06   ` Eric Blake
2014-01-31 15:53 ` [Qemu-devel] [PATCH 2/2] Use g_new() & friends where that makes obvious sense Markus Armbruster
2014-01-31 17:10   ` Eric Blake [this message]
2014-01-31 19:13     ` Richard Henderson
2014-02-03  8:56     ` Markus Armbruster
2014-01-31 16:13 ` [Qemu-devel] [PATCH 0/2] " Peter Maydell
2014-02-03  8:40   ` Markus Armbruster
2014-02-03  9:11     ` Peter Maydell
2014-02-04 16:52       ` Markus Armbruster
2014-02-04 17:27         ` Paolo Bonzini
2014-02-13 16:36       ` Markus Armbruster
2014-02-13 16:48         ` Peter Maydell
2014-02-13 19:30           ` Markus Armbruster
2014-02-18 15:32             ` Markus Armbruster
2014-02-21 11:10               ` Peter Maydell
2014-02-21 11:49                 ` Markus Armbruster
2014-02-21 11:52                   ` Peter Maydell
2014-02-21 12:37                     ` Markus Armbruster

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=52EBD8F9.4080800@redhat.com \
    --to=eblake@redhat.com \
    --cc=agraf@suse.de \
    --cc=aliguori@amazon.com \
    --cc=armbru@redhat.com \
    --cc=mdroth@linux.vnet.ibm.com \
    --cc=peter.maydell@linaro.org \
    --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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).