From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:55147) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1W9Hbt-0007ps-Bg for qemu-devel@nongnu.org; Fri, 31 Jan 2014 12:10:38 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1W9Hbo-0004e5-OF for qemu-devel@nongnu.org; Fri, 31 Jan 2014 12:10:33 -0500 Received: from mx1.redhat.com ([209.132.183.28]:23724) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1W9Hbo-0004dm-F9 for qemu-devel@nongnu.org; Fri, 31 Jan 2014 12:10:28 -0500 Message-ID: <52EBD8F9.4080800@redhat.com> Date: Fri, 31 Jan 2014 10:10:17 -0700 From: Eric Blake MIME-Version: 1.0 References: <1391183604-21621-1-git-send-email-armbru@redhat.com> <1391183604-21621-3-git-send-email-armbru@redhat.com> In-Reply-To: <1391183604-21621-3-git-send-email-armbru@redhat.com> Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="7IV3qLKUljhuS0vUb5tpQvsAIB716b0Av" Subject: Re: [Qemu-devel] [PATCH 2/2] Use g_new() & friends where that makes obvious sense List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Markus Armbruster , qemu-devel@nongnu.org Cc: peter.maydell@linaro.org, mdroth@linux.vnet.ibm.com, aliguori@amazon.com, agraf@suse.de This is an OpenPGP/MIME signed message (RFC 4880 and 3156) --7IV3qLKUljhuS0vUb5tpQvsAIB716b0Av Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable 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. >=20 > Patch created with the following Coccinelle patch, with two hunks > dropped from the result: >=20 Looks like a reasonable formula for replacement. >=20 > Signed-off-by: Markus Armbruster > --- > 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, QEMUS= napshotInfo **psn_tab) > return s->nb_snapshots; > } > =20 > - sn_tab =3D g_malloc0(s->nb_snapshots * sizeof(QEMUSnapshotInfo)); > + sn_tab =3D g_new0(QEMUSnapshotInfo, s->nb_snapshots); > for(i =3D 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(DeviceSta= te *dev, Error **errp) > QTAILQ_INIT(&vser->ports); > =20 > vser->bus.max_nr_ports =3D vser->serial.max_virtserial_ports; > - vser->ivqs =3D g_malloc(vser->serial.max_virtserial_ports > - * sizeof(VirtQueue *)); > - vser->ovqs =3D g_malloc(vser->serial.max_virtserial_ports > - * sizeof(VirtQueue *)); > + vser->ivqs =3D g_new(VirtQueue *, vser->serial.max_virtserial_port= s); > + vser->ovqs =3D g_new(VirtQueue *, vser->serial.max_virtserial_port= s); I'm impressed at what Coccinelle can rewrite! > +++ b/ui/keymaps.c > @@ -107,7 +107,7 @@ static kbd_layout_t *parse_keyboard_layout(const na= me2keysym_t *table, > } > =20 > if (!k) > - k =3D g_malloc0(sizeof(kbd_layout_t)); > + k =3D g_new0(kbd_layout_t, 1); > =20 > 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 --=20 Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org --7IV3qLKUljhuS0vUb5tpQvsAIB716b0Av Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v1 Comment: Public key at http://people.redhat.com/eblake/eblake.gpg Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/ iQEcBAEBCAAGBQJS69j5AAoJEKeha0olJ0NqFRMH/RPQ49NGoUKbGMQHnZlJkJkG Nk5mpA50sK+QIDHj5j0hiLZBSE/lVcq7qoMdjvOY3eiiBiFlfewtBzGHfCdzQ2LH 9CpnQAE630+Tqy7EVpC5Wb8a73NAomzLN76Owa5d0VaGJZURpXh4AyUrElXdF1Sp Q4dBLAQt4DZVAivB9nonLgpy20rJ5gIQBaBTz8blGMThVzffISyhhAxEc9gOE0QZ ZgkxYATVwLp8+rY7AeEGkgFfnG3J/8ttW0LybBKVkjcU/bmZzvb1dM3Ps96/oIf3 NPbCTomVtN9zV/POuJIC2FO0egoqPzewBU1yrAD8R185/1TrhsTvPYWEexO+AtQ= =PhcM -----END PGP SIGNATURE----- --7IV3qLKUljhuS0vUb5tpQvsAIB716b0Av--