qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Qemu-devel] [PATCH] ui: Use g_new() & friends where that makes obvi


From: Eric Blake
Subject: Re: [Qemu-devel] [PATCH] ui: Use g_new() & friends where that makes obvious sense
Date: Mon, 14 Sep 2015 09:30:01 -0600
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.2.0

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 <address@hidden>
> ---
>  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 <address@hidden>

> 
> 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: signature.asc
Description: OpenPGP digital signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]