From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:36381) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZbXoI-0003S1-MJ for qemu-devel@nongnu.org; Mon, 14 Sep 2015 13:44:59 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZbXoE-00034b-K2 for qemu-devel@nongnu.org; Mon, 14 Sep 2015 13:44:58 -0400 Received: from mx1.redhat.com ([209.132.183.28]:44721) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZbXoE-00032y-El for qemu-devel@nongnu.org; Mon, 14 Sep 2015 13:44:54 -0400 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) by mx1.redhat.com (Postfix) with ESMTPS id 6B3ECAB4 for ; Mon, 14 Sep 2015 17:44:53 +0000 (UTC) From: Markus Armbruster References: <1442228637-31481-3-git-send-email-armbru@redhat.com> <55F6E7F9.5010602@redhat.com> Date: Mon, 14 Sep 2015 19:44:50 +0200 In-Reply-To: <55F6E7F9.5010602@redhat.com> (Eric Blake's message of "Mon, 14 Sep 2015 09:30:01 -0600") Message-ID: <87io7cevzh.fsf@blackfin.pond.sub.org> MIME-Version: 1.0 Content-Type: text/plain Subject: Re: [Qemu-devel] [PATCH] ui: Use g_new() & friends where that makes obvious sense List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Eric Blake Cc: qemu-devel@nongnu.org, kraxel@redhat.com Eric Blake writes: > On 09/14/2015 05:03 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. >> >> This commit only touches allocations with size arguments of the form >> sizeof(T). Same Coccinelle semantic patch as in commit b45c03f. >> >> Signed-off-by: Markus Armbruster >> --- >> ui/console.c | 2 +- >> ui/curses.c | 2 +- >> ui/input-legacy.c | 4 ++-- >> ui/keymaps.c | 2 +- >> ui/sdl.c | 2 +- >> ui/vnc-jobs.c | 6 +++--- >> ui/vnc.c | 6 +++--- >> 7 files changed, 12 insertions(+), 12 deletions(-) > > Reviewed-by: Eric Blake > >> >> diff --git a/ui/console.c b/ui/console.c >> index 75fc492..6edda1e 100644 >> --- a/ui/console.c >> +++ b/ui/console.c >> @@ -449,7 +449,7 @@ static void text_console_resize(QemuConsole *s) >> if (s->width < w1) >> w1 = s->width; >> >> - cells = g_malloc(s->width * s->total_height * sizeof(TextCell)); >> + cells = g_new(TextCell, s->width * s->total_height); > > Hopefully s->width * s->total_height can't overflow. Two billion is a helluva lot of character cells :) >> @@ -3025,7 +3025,7 @@ static void vnc_connect(VncDisplay *vd, int csock, >> >> vs->lossy_rect = g_malloc0(VNC_STAT_ROWS * sizeof (*vs->lossy_rect)); >> for (i = 0; i < VNC_STAT_ROWS; ++i) { >> - vs->lossy_rect[i] = g_malloc0(VNC_STAT_COLS * sizeof (uint8_t)); >> + vs->lossy_rect[i] = g_new0(uint8_t, VNC_STAT_COLS); > > sizeof(uint8_t) == 1, according to POSIX. This could be further > simplified to g_malloc0(VNC_STAT_COLS). But if someone wants to do that > simplification, it should be a separate patch. g_new0(uint8_t, VNC_STAT_COLS) returns uint8_t *. g_malloc0(VNC_STAT_COLS) returns void *. Transforming the former to the latter loses a bit of type checking.