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: kraxel@redhat.com
Subject: Re: [Qemu-devel] [PATCH] ui: Use g_new() & friends where that makes obvious sense
Date: Mon, 14 Sep 2015 09:30:01 -0600	[thread overview]
Message-ID: <55F6E7F9.5010602@redhat.com> (raw)
In-Reply-To: <1442228637-31481-3-git-send-email-armbru@redhat.com>

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

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.

> @@ -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.

-- 
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:[~2015-09-14 15:30 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 [this message]
2015-09-14 17:44   ` 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=55F6E7F9.5010602@redhat.com \
    --to=eblake@redhat.com \
    --cc=armbru@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 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).