From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:47726) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zf4IZ-0003aD-9T for qemu-devel@nongnu.org; Thu, 24 Sep 2015 07:02:48 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Zf4IW-00072g-2W for qemu-devel@nongnu.org; Thu, 24 Sep 2015 07:02:47 -0400 Received: from e34.co.us.ibm.com ([32.97.110.152]:32885) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zf4IV-00071x-QO for qemu-devel@nongnu.org; Thu, 24 Sep 2015 07:02:44 -0400 Received: from localhost by e34.co.us.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Thu, 24 Sep 2015 05:02:41 -0600 Received: from b03cxnp07028.gho.boulder.ibm.com (b03cxnp07028.gho.boulder.ibm.com [9.17.130.15]) by d03dlp03.boulder.ibm.com (Postfix) with ESMTP id C6ABD19D8045 for ; Thu, 24 Sep 2015 04:53:31 -0600 (MDT) Received: from d03av03.boulder.ibm.com (d03av03.boulder.ibm.com [9.17.195.169]) by b03cxnp07028.gho.boulder.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id t8OAxYTl62849250 for ; Thu, 24 Sep 2015 03:59:34 -0700 Received: from d03av03.boulder.ibm.com (localhost [127.0.0.1]) by d03av03.boulder.ibm.com (8.14.4/8.14.4/NCO v10.0 AVout) with ESMTP id t8OB2c7a000991 for ; Thu, 24 Sep 2015 05:02:38 -0600 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Michael Roth References: <1442231444-23257-1-git-send-email-armbru@redhat.com> <87pp18ti3y.fsf@blackfin.pond.sub.org> In-Reply-To: <87pp18ti3y.fsf@blackfin.pond.sub.org> Message-ID: <20150924110225.10353.92250@loki> Date: Thu, 24 Sep 2015 06:02:25 -0500 Subject: Re: [Qemu-devel] [PATCH] qga: Use g_new() & friends where that makes obvious sense List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Markus Armbruster , qemu-devel@nongnu.org Quoting Markus Armbruster (2015-09-24 02:02:41) > Ping? Thanks, applied to qga tree: https://github.com/mdroth/qemu/commits/qga > = > Markus Armbruster 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 > > --- > > 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 =3D g_malloc0(sizeof(GAChannel)); > > + GAChannel *c =3D g_new0(GAChannel, 1); > > c->event_cb =3D cb; > > c->user_data =3D 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, GACha= nnelMethod method, > > GAChannel *ga_channel_new(GAChannelMethod method, const gchar *path, > > GAChannelCallback cb, gpointer opaque) > > { > > - GAChannel *c =3D g_malloc0(sizeof(GAChannel)); > > + GAChannel *c =3D 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, Erro= r **errp) > > return -1; > > } > > = > > - gfh =3D g_malloc0(sizeof(GuestFileHandle)); > > + gfh =3D g_new0(GuestFileHandle, 1); > > gfh->id =3D handle; > > gfh->fh =3D fh; > > QTAILQ_INSERT_TAIL(&guest_file_state.filehandles, gfh, next); > > @@ -488,7 +488,7 @@ struct GuestFileRead *qmp_guest_file_read(int64_t h= andle, bool has_count, > > slog("guest-file-read failed, handle: %" PRId64, handle); > > } else { > > buf[read_count] =3D 0; > > - read_data =3D g_malloc0(sizeof(GuestFileRead)); > > + read_data =3D g_new0(GuestFileRead, 1); > > read_data->count =3D read_count; > > read_data->eof =3D 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 =3D g_malloc0(sizeof(GuestFileWrite)); > > + write_data =3D g_new0(GuestFileWrite, 1); > > write_data->count =3D write_count; > > write_data->eof =3D feof(fh); > > } > > @@ -678,7 +678,7 @@ static void build_fs_mount_list_from_mtab(FsMountLi= st *mounts, Error **errp) > > continue; > > } > > = > > - mount =3D g_malloc0(sizeof(FsMount)); > > + mount =3D g_new0(FsMount, 1); > > mount->dirname =3D g_strdup(ment->mnt_dir); > > mount->devtype =3D g_strdup(ment->mnt_type); > > mount->devmajor =3D devmajor; > > @@ -757,7 +757,7 @@ static void build_fs_mount_list(FsMountList *mounts= , Error **errp) > > } > > } > > = > > - mount =3D g_malloc0(sizeof(FsMount)); > > + mount =3D g_new0(FsMount, 1); > > mount->dirname =3D g_strdup(line + dir_s); > > mount->devtype =3D g_strdup(dash + type_s); > > mount->devmajor =3D 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, Err= or **errp) > > if (handle < 0) { > > return -1; > > } > > - gfh =3D g_malloc0(sizeof(GuestFileHandle)); > > + gfh =3D g_new0(GuestFileHandle, 1); > > gfh->id =3D handle; > > gfh->fh =3D 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] =3D 0; > > - read_data =3D g_malloc0(sizeof(GuestFileRead)); > > + read_data =3D g_new0(GuestFileRead, 1); > > read_data->count =3D (size_t)read_count; > > read_data->eof =3D read_count =3D=3D 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 fil= e"); > > slog("guest-file-write-failed, handle: %" PRId64, handle); > > } else { > > - write_data =3D g_malloc0(sizeof(GuestFileWrite)); > > + write_data =3D g_new0(GuestFileWrite, 1); > > write_data->count =3D (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 =3D NULL; > > - GuestSuspendMode *mode =3D g_malloc(sizeof(GuestSuspendMode)); > > + GuestSuspendMode *mode =3D g_new(GuestSuspendMode, 1); > > = > > *mode =3D 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 =3D NULL; > > - GuestSuspendMode *mode =3D g_malloc(sizeof(GuestSuspendMode)); > > + GuestSuspendMode *mode =3D g_new(GuestSuspendMode, 1); > > = > > *mode =3D 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 =3D g_malloc0(sizeof(GuestAgentCommandInfo)); > > + cmd_info =3D g_new0(GuestAgentCommandInfo, 1); > > cmd_info->name =3D g_strdup(qmp_command_name(cmd)); > > cmd_info->enabled =3D qmp_command_is_enabled(cmd); > > cmd_info->success_response =3D qmp_has_success_response(cmd); > > = > > - cmd_info_list =3D g_malloc0(sizeof(GuestAgentCommandInfoList)); > > + cmd_info_list =3D g_new0(GuestAgentCommandInfoList, 1); > > cmd_info_list->value =3D cmd_info; > > cmd_info_list->next =3D info->supported_commands; > > info->supported_commands =3D cmd_info_list; > > @@ -64,7 +64,7 @@ static void qmp_command_info(QmpCommand *cmd, void *o= paque) > > = > > struct GuestAgentInfo *qmp_guest_info(Error **errp) > > { > > - GuestAgentInfo *info =3D g_malloc0(sizeof(GuestAgentInfo)); > > + GuestAgentInfo *info =3D g_new0(GuestAgentInfo, 1); > > = > > info->version =3D 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 =3D g_malloc0(sizeof(GACommandGroup)); > > + GACommandGroup *cg =3D g_new0(GACommandGroup, 1); > > cg->init =3D init; > > cg->cleanup =3D cleanup; > > cs->groups =3D 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 =3D g_malloc0(sizeof(GACommandState)); > > + GACommandState *cs =3D g_new0(GACommandState, 1); > > cs->groups =3D NULL; > > return cs; > > } >=20