diff mbox

qga: Use g_new() & friends where that makes obvious sense

Message ID 1442231444-23257-1-git-send-email-armbru@redhat.com
State New
Headers show

Commit Message

Markus Armbruster Sept. 14, 2015, 11:50 a.m. UTC
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>
---
 qga/channel-posix.c             |  2 +-
 qga/channel-win32.c             |  2 +-
 qga/commands-posix.c            | 10 +++++-----
 qga/commands-win32.c            | 10 +++++-----
 qga/commands.c                  |  6 +++---
 qga/guest-agent-command-state.c |  4 ++--
 6 files changed, 17 insertions(+), 17 deletions(-)

Comments

Eric Blake Sept. 14, 2015, 3:50 p.m. UTC | #1
On 09/14/2015 05:50 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>
> ---
>  qga/channel-posix.c             |  2 +-
>  qga/channel-win32.c             |  2 +-
>  qga/commands-posix.c            | 10 +++++-----
>  qga/commands-win32.c            | 10 +++++-----
>  qga/commands.c                  |  6 +++---
>  qga/guest-agent-command-state.c |  4 ++--
>  6 files changed, 17 insertions(+), 17 deletions(-)

Reviewed-by: Eric Blake <eblake@redhat.com>
Markus Armbruster Sept. 24, 2015, 7:02 a.m. UTC | #2
Ping?

Markus Armbruster <armbru@redhat.com> writes:

> 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>
> ---
>  qga/channel-posix.c             |  2 +-
>  qga/channel-win32.c             |  2 +-
>  qga/commands-posix.c            | 10 +++++-----
>  qga/commands-win32.c            | 10 +++++-----
>  qga/commands.c                  |  6 +++---
>  qga/guest-agent-command-state.c |  4 ++--
>  6 files changed, 17 insertions(+), 17 deletions(-)
>
> diff --git a/qga/channel-posix.c b/qga/channel-posix.c
> index 8aad4fe..61aa3cb 100644
> --- a/qga/channel-posix.c
> +++ b/qga/channel-posix.c
> @@ -249,7 +249,7 @@ GIOStatus ga_channel_read(GAChannel *c, gchar *buf, gsize size, gsize *count)
>  GAChannel *ga_channel_new(GAChannelMethod method, const gchar *path,
>                            GAChannelCallback cb, gpointer opaque)
>  {
> -    GAChannel *c = g_malloc0(sizeof(GAChannel));
> +    GAChannel *c = g_new0(GAChannel, 1);
>      c->event_cb = cb;
>      c->user_data = opaque;
>  
> diff --git a/qga/channel-win32.c b/qga/channel-win32.c
> index 04fa5e4..215b15a 100644
> --- a/qga/channel-win32.c
> +++ b/qga/channel-win32.c
> @@ -322,7 +322,7 @@ static gboolean ga_channel_open(GAChannel *c, GAChannelMethod method,
>  GAChannel *ga_channel_new(GAChannelMethod method, const gchar *path,
>                            GAChannelCallback cb, gpointer opaque)
>  {
> -    GAChannel *c = g_malloc0(sizeof(GAChannel));
> +    GAChannel *c = g_new0(GAChannel, 1);
>      SECURITY_ATTRIBUTES sec_attrs;
>  
>      if (!ga_channel_open(c, method, path)) {
> diff --git a/qga/commands-posix.c b/qga/commands-posix.c
> index b03c316..a932809 100644
> --- a/qga/commands-posix.c
> +++ b/qga/commands-posix.c
> @@ -235,7 +235,7 @@ static int64_t guest_file_handle_add(FILE *fh, Error **errp)
>          return -1;
>      }
>  
> -    gfh = g_malloc0(sizeof(GuestFileHandle));
> +    gfh = g_new0(GuestFileHandle, 1);
>      gfh->id = handle;
>      gfh->fh = fh;
>      QTAILQ_INSERT_TAIL(&guest_file_state.filehandles, gfh, next);
> @@ -488,7 +488,7 @@ struct GuestFileRead *qmp_guest_file_read(int64_t handle, bool has_count,
>          slog("guest-file-read failed, handle: %" PRId64, handle);
>      } else {
>          buf[read_count] = 0;
> -        read_data = g_malloc0(sizeof(GuestFileRead));
> +        read_data = g_new0(GuestFileRead, 1);
>          read_data->count = read_count;
>          read_data->eof = feof(fh);
>          if (read_count) {
> @@ -533,7 +533,7 @@ GuestFileWrite *qmp_guest_file_write(int64_t handle, const char *buf_b64,
>          error_setg_errno(errp, errno, "failed to write to file");
>          slog("guest-file-write failed, handle: %" PRId64, handle);
>      } else {
> -        write_data = g_malloc0(sizeof(GuestFileWrite));
> +        write_data = g_new0(GuestFileWrite, 1);
>          write_data->count = write_count;
>          write_data->eof = feof(fh);
>      }
> @@ -678,7 +678,7 @@ static void build_fs_mount_list_from_mtab(FsMountList *mounts, Error **errp)
>              continue;
>          }
>  
> -        mount = g_malloc0(sizeof(FsMount));
> +        mount = g_new0(FsMount, 1);
>          mount->dirname = g_strdup(ment->mnt_dir);
>          mount->devtype = g_strdup(ment->mnt_type);
>          mount->devmajor = devmajor;
> @@ -757,7 +757,7 @@ static void build_fs_mount_list(FsMountList *mounts, Error **errp)
>              }
>          }
>  
> -        mount = g_malloc0(sizeof(FsMount));
> +        mount = g_new0(FsMount, 1);
>          mount->dirname = g_strdup(line + dir_s);
>          mount->devtype = g_strdup(dash + type_s);
>          mount->devmajor = devmajor;
> diff --git a/qga/commands-win32.c b/qga/commands-win32.c
> index 41bdd3f..0481646 100644
> --- a/qga/commands-win32.c
> +++ b/qga/commands-win32.c
> @@ -106,7 +106,7 @@ static int64_t guest_file_handle_add(HANDLE fh, Error **errp)
>      if (handle < 0) {
>          return -1;
>      }
> -    gfh = g_malloc0(sizeof(GuestFileHandle));
> +    gfh = g_new0(GuestFileHandle, 1);
>      gfh->id = handle;
>      gfh->fh = fh;
>      QTAILQ_INSERT_TAIL(&guest_file_state.filehandles, gfh, next);
> @@ -298,7 +298,7 @@ GuestFileRead *qmp_guest_file_read(int64_t handle, bool has_count,
>          slog("guest-file-read failed, handle %" PRId64, handle);
>      } else {
>          buf[read_count] = 0;
> -        read_data = g_malloc0(sizeof(GuestFileRead));
> +        read_data = g_new0(GuestFileRead, 1);
>          read_data->count = (size_t)read_count;
>          read_data->eof = read_count == 0;
>  
> @@ -342,7 +342,7 @@ GuestFileWrite *qmp_guest_file_write(int64_t handle, const char *buf_b64,
>          error_setg_win32(errp, GetLastError(), "failed to write to file");
>          slog("guest-file-write-failed, handle: %" PRId64, handle);
>      } else {
> -        write_data = g_malloc0(sizeof(GuestFileWrite));
> +        write_data = g_new0(GuestFileWrite, 1);
>          write_data->count = (size_t) write_count;
>      }
>  
> @@ -865,7 +865,7 @@ static DWORD WINAPI do_suspend(LPVOID opaque)
>  void qmp_guest_suspend_disk(Error **errp)
>  {
>      Error *local_err = NULL;
> -    GuestSuspendMode *mode = g_malloc(sizeof(GuestSuspendMode));
> +    GuestSuspendMode *mode = g_new(GuestSuspendMode, 1);
>  
>      *mode = GUEST_SUSPEND_MODE_DISK;
>      check_suspend_mode(*mode, &local_err);
> @@ -881,7 +881,7 @@ void qmp_guest_suspend_disk(Error **errp)
>  void qmp_guest_suspend_ram(Error **errp)
>  {
>      Error *local_err = NULL;
> -    GuestSuspendMode *mode = g_malloc(sizeof(GuestSuspendMode));
> +    GuestSuspendMode *mode = g_new(GuestSuspendMode, 1);
>  
>      *mode = GUEST_SUSPEND_MODE_RAM;
>      check_suspend_mode(*mode, &local_err);
> diff --git a/qga/commands.c b/qga/commands.c
> index 7834967..9a765e8 100644
> --- a/qga/commands.c
> +++ b/qga/commands.c
> @@ -51,12 +51,12 @@ static void qmp_command_info(QmpCommand *cmd, void *opaque)
>      GuestAgentCommandInfo *cmd_info;
>      GuestAgentCommandInfoList *cmd_info_list;
>  
> -    cmd_info = g_malloc0(sizeof(GuestAgentCommandInfo));
> +    cmd_info = g_new0(GuestAgentCommandInfo, 1);
>      cmd_info->name = g_strdup(qmp_command_name(cmd));
>      cmd_info->enabled = qmp_command_is_enabled(cmd);
>      cmd_info->success_response = qmp_has_success_response(cmd);
>  
> -    cmd_info_list = g_malloc0(sizeof(GuestAgentCommandInfoList));
> +    cmd_info_list = g_new0(GuestAgentCommandInfoList, 1);
>      cmd_info_list->value = cmd_info;
>      cmd_info_list->next = info->supported_commands;
>      info->supported_commands = cmd_info_list;
> @@ -64,7 +64,7 @@ static void qmp_command_info(QmpCommand *cmd, void *opaque)
>  
>  struct GuestAgentInfo *qmp_guest_info(Error **errp)
>  {
> -    GuestAgentInfo *info = g_malloc0(sizeof(GuestAgentInfo));
> +    GuestAgentInfo *info = g_new0(GuestAgentInfo, 1);
>  
>      info->version = g_strdup(QEMU_VERSION);
>      qmp_for_each_command(qmp_command_info, info);
> diff --git a/qga/guest-agent-command-state.c b/qga/guest-agent-command-state.c
> index 969da23..128c549 100644
> --- a/qga/guest-agent-command-state.c
> +++ b/qga/guest-agent-command-state.c
> @@ -27,7 +27,7 @@ void ga_command_state_add(GACommandState *cs,
>                            void (*init)(void),
>                            void (*cleanup)(void))
>  {
> -    GACommandGroup *cg = g_malloc0(sizeof(GACommandGroup));
> +    GACommandGroup *cg = g_new0(GACommandGroup, 1);
>      cg->init = init;
>      cg->cleanup = cleanup;
>      cs->groups = g_slist_append(cs->groups, cg);
> @@ -67,7 +67,7 @@ void ga_command_state_cleanup_all(GACommandState *cs)
>  
>  GACommandState *ga_command_state_new(void)
>  {
> -    GACommandState *cs = g_malloc0(sizeof(GACommandState));
> +    GACommandState *cs = g_new0(GACommandState, 1);
>      cs->groups = NULL;
>      return cs;
>  }
Michael Roth Sept. 24, 2015, 11:02 a.m. UTC | #3
Quoting Markus Armbruster (2015-09-24 02:02:41)
> Ping?

Thanks, applied to qga tree:

  https://github.com/mdroth/qemu/commits/qga

> 
> Markus Armbruster <armbru@redhat.com> writes:
> 
> > 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>
> > ---
> >  qga/channel-posix.c             |  2 +-
> >  qga/channel-win32.c             |  2 +-
> >  qga/commands-posix.c            | 10 +++++-----
> >  qga/commands-win32.c            | 10 +++++-----
> >  qga/commands.c                  |  6 +++---
> >  qga/guest-agent-command-state.c |  4 ++--
> >  6 files changed, 17 insertions(+), 17 deletions(-)
> >
> > diff --git a/qga/channel-posix.c b/qga/channel-posix.c
> > index 8aad4fe..61aa3cb 100644
> > --- a/qga/channel-posix.c
> > +++ b/qga/channel-posix.c
> > @@ -249,7 +249,7 @@ GIOStatus ga_channel_read(GAChannel *c, gchar *buf, gsize size, gsize *count)
> >  GAChannel *ga_channel_new(GAChannelMethod method, const gchar *path,
> >                            GAChannelCallback cb, gpointer opaque)
> >  {
> > -    GAChannel *c = g_malloc0(sizeof(GAChannel));
> > +    GAChannel *c = g_new0(GAChannel, 1);
> >      c->event_cb = cb;
> >      c->user_data = opaque;
> >  
> > diff --git a/qga/channel-win32.c b/qga/channel-win32.c
> > index 04fa5e4..215b15a 100644
> > --- a/qga/channel-win32.c
> > +++ b/qga/channel-win32.c
> > @@ -322,7 +322,7 @@ static gboolean ga_channel_open(GAChannel *c, GAChannelMethod method,
> >  GAChannel *ga_channel_new(GAChannelMethod method, const gchar *path,
> >                            GAChannelCallback cb, gpointer opaque)
> >  {
> > -    GAChannel *c = g_malloc0(sizeof(GAChannel));
> > +    GAChannel *c = g_new0(GAChannel, 1);
> >      SECURITY_ATTRIBUTES sec_attrs;
> >  
> >      if (!ga_channel_open(c, method, path)) {
> > diff --git a/qga/commands-posix.c b/qga/commands-posix.c
> > index b03c316..a932809 100644
> > --- a/qga/commands-posix.c
> > +++ b/qga/commands-posix.c
> > @@ -235,7 +235,7 @@ static int64_t guest_file_handle_add(FILE *fh, Error **errp)
> >          return -1;
> >      }
> >  
> > -    gfh = g_malloc0(sizeof(GuestFileHandle));
> > +    gfh = g_new0(GuestFileHandle, 1);
> >      gfh->id = handle;
> >      gfh->fh = fh;
> >      QTAILQ_INSERT_TAIL(&guest_file_state.filehandles, gfh, next);
> > @@ -488,7 +488,7 @@ struct GuestFileRead *qmp_guest_file_read(int64_t handle, bool has_count,
> >          slog("guest-file-read failed, handle: %" PRId64, handle);
> >      } else {
> >          buf[read_count] = 0;
> > -        read_data = g_malloc0(sizeof(GuestFileRead));
> > +        read_data = g_new0(GuestFileRead, 1);
> >          read_data->count = read_count;
> >          read_data->eof = feof(fh);
> >          if (read_count) {
> > @@ -533,7 +533,7 @@ GuestFileWrite *qmp_guest_file_write(int64_t handle, const char *buf_b64,
> >          error_setg_errno(errp, errno, "failed to write to file");
> >          slog("guest-file-write failed, handle: %" PRId64, handle);
> >      } else {
> > -        write_data = g_malloc0(sizeof(GuestFileWrite));
> > +        write_data = g_new0(GuestFileWrite, 1);
> >          write_data->count = write_count;
> >          write_data->eof = feof(fh);
> >      }
> > @@ -678,7 +678,7 @@ static void build_fs_mount_list_from_mtab(FsMountList *mounts, Error **errp)
> >              continue;
> >          }
> >  
> > -        mount = g_malloc0(sizeof(FsMount));
> > +        mount = g_new0(FsMount, 1);
> >          mount->dirname = g_strdup(ment->mnt_dir);
> >          mount->devtype = g_strdup(ment->mnt_type);
> >          mount->devmajor = devmajor;
> > @@ -757,7 +757,7 @@ static void build_fs_mount_list(FsMountList *mounts, Error **errp)
> >              }
> >          }
> >  
> > -        mount = g_malloc0(sizeof(FsMount));
> > +        mount = g_new0(FsMount, 1);
> >          mount->dirname = g_strdup(line + dir_s);
> >          mount->devtype = g_strdup(dash + type_s);
> >          mount->devmajor = devmajor;
> > diff --git a/qga/commands-win32.c b/qga/commands-win32.c
> > index 41bdd3f..0481646 100644
> > --- a/qga/commands-win32.c
> > +++ b/qga/commands-win32.c
> > @@ -106,7 +106,7 @@ static int64_t guest_file_handle_add(HANDLE fh, Error **errp)
> >      if (handle < 0) {
> >          return -1;
> >      }
> > -    gfh = g_malloc0(sizeof(GuestFileHandle));
> > +    gfh = g_new0(GuestFileHandle, 1);
> >      gfh->id = handle;
> >      gfh->fh = fh;
> >      QTAILQ_INSERT_TAIL(&guest_file_state.filehandles, gfh, next);
> > @@ -298,7 +298,7 @@ GuestFileRead *qmp_guest_file_read(int64_t handle, bool has_count,
> >          slog("guest-file-read failed, handle %" PRId64, handle);
> >      } else {
> >          buf[read_count] = 0;
> > -        read_data = g_malloc0(sizeof(GuestFileRead));
> > +        read_data = g_new0(GuestFileRead, 1);
> >          read_data->count = (size_t)read_count;
> >          read_data->eof = read_count == 0;
> >  
> > @@ -342,7 +342,7 @@ GuestFileWrite *qmp_guest_file_write(int64_t handle, const char *buf_b64,
> >          error_setg_win32(errp, GetLastError(), "failed to write to file");
> >          slog("guest-file-write-failed, handle: %" PRId64, handle);
> >      } else {
> > -        write_data = g_malloc0(sizeof(GuestFileWrite));
> > +        write_data = g_new0(GuestFileWrite, 1);
> >          write_data->count = (size_t) write_count;
> >      }
> >  
> > @@ -865,7 +865,7 @@ static DWORD WINAPI do_suspend(LPVOID opaque)
> >  void qmp_guest_suspend_disk(Error **errp)
> >  {
> >      Error *local_err = NULL;
> > -    GuestSuspendMode *mode = g_malloc(sizeof(GuestSuspendMode));
> > +    GuestSuspendMode *mode = g_new(GuestSuspendMode, 1);
> >  
> >      *mode = GUEST_SUSPEND_MODE_DISK;
> >      check_suspend_mode(*mode, &local_err);
> > @@ -881,7 +881,7 @@ void qmp_guest_suspend_disk(Error **errp)
> >  void qmp_guest_suspend_ram(Error **errp)
> >  {
> >      Error *local_err = NULL;
> > -    GuestSuspendMode *mode = g_malloc(sizeof(GuestSuspendMode));
> > +    GuestSuspendMode *mode = g_new(GuestSuspendMode, 1);
> >  
> >      *mode = GUEST_SUSPEND_MODE_RAM;
> >      check_suspend_mode(*mode, &local_err);
> > diff --git a/qga/commands.c b/qga/commands.c
> > index 7834967..9a765e8 100644
> > --- a/qga/commands.c
> > +++ b/qga/commands.c
> > @@ -51,12 +51,12 @@ static void qmp_command_info(QmpCommand *cmd, void *opaque)
> >      GuestAgentCommandInfo *cmd_info;
> >      GuestAgentCommandInfoList *cmd_info_list;
> >  
> > -    cmd_info = g_malloc0(sizeof(GuestAgentCommandInfo));
> > +    cmd_info = g_new0(GuestAgentCommandInfo, 1);
> >      cmd_info->name = g_strdup(qmp_command_name(cmd));
> >      cmd_info->enabled = qmp_command_is_enabled(cmd);
> >      cmd_info->success_response = qmp_has_success_response(cmd);
> >  
> > -    cmd_info_list = g_malloc0(sizeof(GuestAgentCommandInfoList));
> > +    cmd_info_list = g_new0(GuestAgentCommandInfoList, 1);
> >      cmd_info_list->value = cmd_info;
> >      cmd_info_list->next = info->supported_commands;
> >      info->supported_commands = cmd_info_list;
> > @@ -64,7 +64,7 @@ static void qmp_command_info(QmpCommand *cmd, void *opaque)
> >  
> >  struct GuestAgentInfo *qmp_guest_info(Error **errp)
> >  {
> > -    GuestAgentInfo *info = g_malloc0(sizeof(GuestAgentInfo));
> > +    GuestAgentInfo *info = g_new0(GuestAgentInfo, 1);
> >  
> >      info->version = g_strdup(QEMU_VERSION);
> >      qmp_for_each_command(qmp_command_info, info);
> > diff --git a/qga/guest-agent-command-state.c b/qga/guest-agent-command-state.c
> > index 969da23..128c549 100644
> > --- a/qga/guest-agent-command-state.c
> > +++ b/qga/guest-agent-command-state.c
> > @@ -27,7 +27,7 @@ void ga_command_state_add(GACommandState *cs,
> >                            void (*init)(void),
> >                            void (*cleanup)(void))
> >  {
> > -    GACommandGroup *cg = g_malloc0(sizeof(GACommandGroup));
> > +    GACommandGroup *cg = g_new0(GACommandGroup, 1);
> >      cg->init = init;
> >      cg->cleanup = cleanup;
> >      cs->groups = g_slist_append(cs->groups, cg);
> > @@ -67,7 +67,7 @@ void ga_command_state_cleanup_all(GACommandState *cs)
> >  
> >  GACommandState *ga_command_state_new(void)
> >  {
> > -    GACommandState *cs = g_malloc0(sizeof(GACommandState));
> > +    GACommandState *cs = g_new0(GACommandState, 1);
> >      cs->groups = NULL;
> >      return cs;
> >  }
>
diff mbox

Patch

diff --git a/qga/channel-posix.c b/qga/channel-posix.c
index 8aad4fe..61aa3cb 100644
--- a/qga/channel-posix.c
+++ b/qga/channel-posix.c
@@ -249,7 +249,7 @@  GIOStatus ga_channel_read(GAChannel *c, gchar *buf, gsize size, gsize *count)
 GAChannel *ga_channel_new(GAChannelMethod method, const gchar *path,
                           GAChannelCallback cb, gpointer opaque)
 {
-    GAChannel *c = g_malloc0(sizeof(GAChannel));
+    GAChannel *c = g_new0(GAChannel, 1);
     c->event_cb = cb;
     c->user_data = opaque;
 
diff --git a/qga/channel-win32.c b/qga/channel-win32.c
index 04fa5e4..215b15a 100644
--- a/qga/channel-win32.c
+++ b/qga/channel-win32.c
@@ -322,7 +322,7 @@  static gboolean ga_channel_open(GAChannel *c, GAChannelMethod method,
 GAChannel *ga_channel_new(GAChannelMethod method, const gchar *path,
                           GAChannelCallback cb, gpointer opaque)
 {
-    GAChannel *c = g_malloc0(sizeof(GAChannel));
+    GAChannel *c = g_new0(GAChannel, 1);
     SECURITY_ATTRIBUTES sec_attrs;
 
     if (!ga_channel_open(c, method, path)) {
diff --git a/qga/commands-posix.c b/qga/commands-posix.c
index b03c316..a932809 100644
--- a/qga/commands-posix.c
+++ b/qga/commands-posix.c
@@ -235,7 +235,7 @@  static int64_t guest_file_handle_add(FILE *fh, Error **errp)
         return -1;
     }
 
-    gfh = g_malloc0(sizeof(GuestFileHandle));
+    gfh = g_new0(GuestFileHandle, 1);
     gfh->id = handle;
     gfh->fh = fh;
     QTAILQ_INSERT_TAIL(&guest_file_state.filehandles, gfh, next);
@@ -488,7 +488,7 @@  struct GuestFileRead *qmp_guest_file_read(int64_t handle, bool has_count,
         slog("guest-file-read failed, handle: %" PRId64, handle);
     } else {
         buf[read_count] = 0;
-        read_data = g_malloc0(sizeof(GuestFileRead));
+        read_data = g_new0(GuestFileRead, 1);
         read_data->count = read_count;
         read_data->eof = feof(fh);
         if (read_count) {
@@ -533,7 +533,7 @@  GuestFileWrite *qmp_guest_file_write(int64_t handle, const char *buf_b64,
         error_setg_errno(errp, errno, "failed to write to file");
         slog("guest-file-write failed, handle: %" PRId64, handle);
     } else {
-        write_data = g_malloc0(sizeof(GuestFileWrite));
+        write_data = g_new0(GuestFileWrite, 1);
         write_data->count = write_count;
         write_data->eof = feof(fh);
     }
@@ -678,7 +678,7 @@  static void build_fs_mount_list_from_mtab(FsMountList *mounts, Error **errp)
             continue;
         }
 
-        mount = g_malloc0(sizeof(FsMount));
+        mount = g_new0(FsMount, 1);
         mount->dirname = g_strdup(ment->mnt_dir);
         mount->devtype = g_strdup(ment->mnt_type);
         mount->devmajor = devmajor;
@@ -757,7 +757,7 @@  static void build_fs_mount_list(FsMountList *mounts, Error **errp)
             }
         }
 
-        mount = g_malloc0(sizeof(FsMount));
+        mount = g_new0(FsMount, 1);
         mount->dirname = g_strdup(line + dir_s);
         mount->devtype = g_strdup(dash + type_s);
         mount->devmajor = devmajor;
diff --git a/qga/commands-win32.c b/qga/commands-win32.c
index 41bdd3f..0481646 100644
--- a/qga/commands-win32.c
+++ b/qga/commands-win32.c
@@ -106,7 +106,7 @@  static int64_t guest_file_handle_add(HANDLE fh, Error **errp)
     if (handle < 0) {
         return -1;
     }
-    gfh = g_malloc0(sizeof(GuestFileHandle));
+    gfh = g_new0(GuestFileHandle, 1);
     gfh->id = handle;
     gfh->fh = fh;
     QTAILQ_INSERT_TAIL(&guest_file_state.filehandles, gfh, next);
@@ -298,7 +298,7 @@  GuestFileRead *qmp_guest_file_read(int64_t handle, bool has_count,
         slog("guest-file-read failed, handle %" PRId64, handle);
     } else {
         buf[read_count] = 0;
-        read_data = g_malloc0(sizeof(GuestFileRead));
+        read_data = g_new0(GuestFileRead, 1);
         read_data->count = (size_t)read_count;
         read_data->eof = read_count == 0;
 
@@ -342,7 +342,7 @@  GuestFileWrite *qmp_guest_file_write(int64_t handle, const char *buf_b64,
         error_setg_win32(errp, GetLastError(), "failed to write to file");
         slog("guest-file-write-failed, handle: %" PRId64, handle);
     } else {
-        write_data = g_malloc0(sizeof(GuestFileWrite));
+        write_data = g_new0(GuestFileWrite, 1);
         write_data->count = (size_t) write_count;
     }
 
@@ -865,7 +865,7 @@  static DWORD WINAPI do_suspend(LPVOID opaque)
 void qmp_guest_suspend_disk(Error **errp)
 {
     Error *local_err = NULL;
-    GuestSuspendMode *mode = g_malloc(sizeof(GuestSuspendMode));
+    GuestSuspendMode *mode = g_new(GuestSuspendMode, 1);
 
     *mode = GUEST_SUSPEND_MODE_DISK;
     check_suspend_mode(*mode, &local_err);
@@ -881,7 +881,7 @@  void qmp_guest_suspend_disk(Error **errp)
 void qmp_guest_suspend_ram(Error **errp)
 {
     Error *local_err = NULL;
-    GuestSuspendMode *mode = g_malloc(sizeof(GuestSuspendMode));
+    GuestSuspendMode *mode = g_new(GuestSuspendMode, 1);
 
     *mode = GUEST_SUSPEND_MODE_RAM;
     check_suspend_mode(*mode, &local_err);
diff --git a/qga/commands.c b/qga/commands.c
index 7834967..9a765e8 100644
--- a/qga/commands.c
+++ b/qga/commands.c
@@ -51,12 +51,12 @@  static void qmp_command_info(QmpCommand *cmd, void *opaque)
     GuestAgentCommandInfo *cmd_info;
     GuestAgentCommandInfoList *cmd_info_list;
 
-    cmd_info = g_malloc0(sizeof(GuestAgentCommandInfo));
+    cmd_info = g_new0(GuestAgentCommandInfo, 1);
     cmd_info->name = g_strdup(qmp_command_name(cmd));
     cmd_info->enabled = qmp_command_is_enabled(cmd);
     cmd_info->success_response = qmp_has_success_response(cmd);
 
-    cmd_info_list = g_malloc0(sizeof(GuestAgentCommandInfoList));
+    cmd_info_list = g_new0(GuestAgentCommandInfoList, 1);
     cmd_info_list->value = cmd_info;
     cmd_info_list->next = info->supported_commands;
     info->supported_commands = cmd_info_list;
@@ -64,7 +64,7 @@  static void qmp_command_info(QmpCommand *cmd, void *opaque)
 
 struct GuestAgentInfo *qmp_guest_info(Error **errp)
 {
-    GuestAgentInfo *info = g_malloc0(sizeof(GuestAgentInfo));
+    GuestAgentInfo *info = g_new0(GuestAgentInfo, 1);
 
     info->version = g_strdup(QEMU_VERSION);
     qmp_for_each_command(qmp_command_info, info);
diff --git a/qga/guest-agent-command-state.c b/qga/guest-agent-command-state.c
index 969da23..128c549 100644
--- a/qga/guest-agent-command-state.c
+++ b/qga/guest-agent-command-state.c
@@ -27,7 +27,7 @@  void ga_command_state_add(GACommandState *cs,
                           void (*init)(void),
                           void (*cleanup)(void))
 {
-    GACommandGroup *cg = g_malloc0(sizeof(GACommandGroup));
+    GACommandGroup *cg = g_new0(GACommandGroup, 1);
     cg->init = init;
     cg->cleanup = cleanup;
     cs->groups = g_slist_append(cs->groups, cg);
@@ -67,7 +67,7 @@  void ga_command_state_cleanup_all(GACommandState *cs)
 
 GACommandState *ga_command_state_new(void)
 {
-    GACommandState *cs = g_malloc0(sizeof(GACommandState));
+    GACommandState *cs = g_new0(GACommandState, 1);
     cs->groups = NULL;
     return cs;
 }