All of lore.kernel.org
 help / color / mirror / Atom feed
From: Markus Armbruster <armbru@redhat.com>
To: Eric Blake <eblake@redhat.com>
Cc: qemu-devel@nongnu.org, kraxel@redhat.com
Subject: Re: [Qemu-devel] [PATCH] ui: Use g_new() & friends where that makes obvious sense
Date: Mon, 14 Sep 2015 19:44:50 +0200	[thread overview]
Message-ID: <87io7cevzh.fsf@blackfin.pond.sub.org> (raw)
In-Reply-To: <55F6E7F9.5010602@redhat.com> (Eric Blake's message of "Mon, 14 Sep 2015 09:30:01 -0600")

Eric Blake <eblake@redhat.com> 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 <armbru@redhat.com>
>> ---
>>  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 <eblake@redhat.com>
>
>> 
>> 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.

      reply	other threads:[~2015-09-14 17:44 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-09-14 11:03 [Qemu-devel] [PATCH] ui: Use g_new() & friends where that makes obvious sense Markus Armbruster
2015-09-14 15:30 ` Eric Blake
2015-09-14 17:44   ` Markus Armbruster [this message]

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=87io7cevzh.fsf@blackfin.pond.sub.org \
    --to=armbru@redhat.com \
    --cc=eblake@redhat.com \
    --cc=kraxel@redhat.com \
    --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.