From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:40488) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZUJjo-0003bq-Dm for qemu-devel@nongnu.org; Tue, 25 Aug 2015 15:18:29 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZUJjj-00056b-HS for qemu-devel@nongnu.org; Tue, 25 Aug 2015 15:18:28 -0400 Received: from mx1.redhat.com ([209.132.183.28]:53052) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZUJjj-00055R-AE for qemu-devel@nongnu.org; Tue, 25 Aug 2015 15:18:23 -0400 References: <1440524394-15640-1-git-send-email-armbru@redhat.com> From: Eric Blake Message-ID: <55DCBF79.7040709@redhat.com> Date: Tue, 25 Aug 2015 13:18:17 -0600 MIME-Version: 1.0 In-Reply-To: <1440524394-15640-1-git-send-email-armbru@redhat.com> Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="MQTcwCbuqPbI7uTXaCvIoXk1qkMqtCK23" Subject: Re: [Qemu-devel] [PATCH] arm: 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, peter.crosthwaite@xilinx.com This is an OpenPGP/MIME signed message (RFC 4880 and 3156) --MQTcwCbuqPbI7uTXaCvIoXk1qkMqtCK23 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable On 08/25/2015 11:39 AM, Markus Armbruster wrote: > g_new(T, n) is neater than g_malloc(sizeof(T) * n). It's also safer, > 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 > This commit only touches allocations with size arguments of the form > sizeof(T). >=20 > Coccinelle semantic patch: >=20 > @@ > type T; > @@ > -g_malloc(sizeof(T)) > +g_new(T, 1) > @@ > type T; I find it slightly easier to read coccinelle if there is a blank line before every second @@, to call attention to metatype vs. changes (coccinelle doesn't care either way). And it's probably possible to write the coccinelle more succinctly, by grouping similar rules together using something like this (although I didn't actually test it): @@ type T; @@ ( g_malloc | g_try_malloc | g_malloc0 | g_try_malloc0 ) -(sizeof(T)) +(T, 1) but it doesn't matter in the long run. > Signed-off-by: Markus Armbruster > --- Both the coccinelle rule and the resulting conversion look sane. Reviewed-by: Eric Blake > +++ b/hw/gpio/omap_gpio.c > @@ -710,8 +710,8 @@ static int omap2_gpio_init(SysBusDevice *sbd) > } else { > s->modulecount =3D 6; > } > - s->modules =3D g_malloc0(s->modulecount * sizeof(struct omap2_gpio= _s)); > - s->handler =3D g_malloc0(s->modulecount * 32 * sizeof(qemu_irq)); > + s->modules =3D g_new0(struct omap2_gpio_s, s->modulecount); > + s->handler =3D g_new0(qemu_irq, s->modulecount * 32); > qdev_init_gpio_in(dev, omap2_gpio_set, s->modulecount * 32); > qdev_init_gpio_out(dev, s->handler, s->modulecount * 32); Thankfully, the '* 32' here does not overflow even pre-patch, since s->modulecount was set to a maximum of 6 in the preceding if/else block. --=20 Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org --MQTcwCbuqPbI7uTXaCvIoXk1qkMqtCK23 Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v2 Comment: Public key at http://people.redhat.com/eblake/eblake.gpg Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/ iQEcBAEBCAAGBQJV3L95AAoJEKeha0olJ0Nq5F8H/0WARATm/XMgloFHUxb1XzoD CcUXc6fgcQBTbWcK1z/hG3H5zujc+kN5RRX8jIGdQX4t1jv33e59ynYNEdaAHP77 jXJzlKqmWju/bvtdyozMBfgXDcitjWJ3RQnpwdk3NsE3j1qkFdSM0qimCKPujlzp OrA1RAibB91mklYDDqWmuKEKye+Z+B70QT6POD9fXxdW7ULGOFNPENRKuePM3gX9 RceAGHRoELpob2X3VihxkwhfQnOY4lUivfCpCIfpBanVeqtNMCNPsDjknL7hdbDZ t4uB8wNCTWwL3N0RaikeMTFz8M2wICk7xqYybb0rolLw1OYbSBCxA4KU3TcoUPM= =XexP -----END PGP SIGNATURE----- --MQTcwCbuqPbI7uTXaCvIoXk1qkMqtCK23--