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

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>
Reviewed-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
This commit is contained in:
Markus Armbruster 2015-09-14 13:50:44 +02:00 committed by Michael Roth
parent fafcaf1d74
commit f3a06403b8
6 changed files with 17 additions and 17 deletions

View File

@ -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, GAChannel *ga_channel_new(GAChannelMethod method, const gchar *path,
GAChannelCallback cb, gpointer opaque) GAChannelCallback cb, gpointer opaque)
{ {
GAChannel *c = g_malloc0(sizeof(GAChannel)); GAChannel *c = g_new0(GAChannel, 1);
c->event_cb = cb; c->event_cb = cb;
c->user_data = opaque; c->user_data = opaque;

View File

@ -322,7 +322,7 @@ static gboolean ga_channel_open(GAChannel *c, GAChannelMethod method,
GAChannel *ga_channel_new(GAChannelMethod method, const gchar *path, GAChannel *ga_channel_new(GAChannelMethod method, const gchar *path,
GAChannelCallback cb, gpointer opaque) GAChannelCallback cb, gpointer opaque)
{ {
GAChannel *c = g_malloc0(sizeof(GAChannel)); GAChannel *c = g_new0(GAChannel, 1);
SECURITY_ATTRIBUTES sec_attrs; SECURITY_ATTRIBUTES sec_attrs;
if (!ga_channel_open(c, method, path)) { if (!ga_channel_open(c, method, path)) {

View File

@ -235,7 +235,7 @@ static int64_t guest_file_handle_add(FILE *fh, Error **errp)
return -1; return -1;
} }
gfh = g_malloc0(sizeof(GuestFileHandle)); gfh = g_new0(GuestFileHandle, 1);
gfh->id = handle; gfh->id = handle;
gfh->fh = fh; gfh->fh = fh;
QTAILQ_INSERT_TAIL(&guest_file_state.filehandles, gfh, next); 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); slog("guest-file-read failed, handle: %" PRId64, handle);
} else { } else {
buf[read_count] = 0; buf[read_count] = 0;
read_data = g_malloc0(sizeof(GuestFileRead)); read_data = g_new0(GuestFileRead, 1);
read_data->count = read_count; read_data->count = read_count;
read_data->eof = feof(fh); read_data->eof = feof(fh);
if (read_count) { 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"); error_setg_errno(errp, errno, "failed to write to file");
slog("guest-file-write failed, handle: %" PRId64, handle); slog("guest-file-write failed, handle: %" PRId64, handle);
} else { } else {
write_data = g_malloc0(sizeof(GuestFileWrite)); write_data = g_new0(GuestFileWrite, 1);
write_data->count = write_count; write_data->count = write_count;
write_data->eof = feof(fh); write_data->eof = feof(fh);
} }
@ -678,7 +678,7 @@ static void build_fs_mount_list_from_mtab(FsMountList *mounts, Error **errp)
continue; continue;
} }
mount = g_malloc0(sizeof(FsMount)); mount = g_new0(FsMount, 1);
mount->dirname = g_strdup(ment->mnt_dir); mount->dirname = g_strdup(ment->mnt_dir);
mount->devtype = g_strdup(ment->mnt_type); mount->devtype = g_strdup(ment->mnt_type);
mount->devmajor = devmajor; 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->dirname = g_strdup(line + dir_s);
mount->devtype = g_strdup(dash + type_s); mount->devtype = g_strdup(dash + type_s);
mount->devmajor = devmajor; mount->devmajor = devmajor;

View File

@ -106,7 +106,7 @@ static int64_t guest_file_handle_add(HANDLE fh, Error **errp)
if (handle < 0) { if (handle < 0) {
return -1; return -1;
} }
gfh = g_malloc0(sizeof(GuestFileHandle)); gfh = g_new0(GuestFileHandle, 1);
gfh->id = handle; gfh->id = handle;
gfh->fh = fh; gfh->fh = fh;
QTAILQ_INSERT_TAIL(&guest_file_state.filehandles, gfh, next); 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); slog("guest-file-read failed, handle %" PRId64, handle);
} else { } else {
buf[read_count] = 0; 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->count = (size_t)read_count;
read_data->eof = read_count == 0; 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"); error_setg_win32(errp, GetLastError(), "failed to write to file");
slog("guest-file-write-failed, handle: %" PRId64, handle); slog("guest-file-write-failed, handle: %" PRId64, handle);
} else { } else {
write_data = g_malloc0(sizeof(GuestFileWrite)); write_data = g_new0(GuestFileWrite, 1);
write_data->count = (size_t) write_count; 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) void qmp_guest_suspend_disk(Error **errp)
{ {
Error *local_err = NULL; Error *local_err = NULL;
GuestSuspendMode *mode = g_malloc(sizeof(GuestSuspendMode)); GuestSuspendMode *mode = g_new(GuestSuspendMode, 1);
*mode = GUEST_SUSPEND_MODE_DISK; *mode = GUEST_SUSPEND_MODE_DISK;
check_suspend_mode(*mode, &local_err); check_suspend_mode(*mode, &local_err);
@ -881,7 +881,7 @@ void qmp_guest_suspend_disk(Error **errp)
void qmp_guest_suspend_ram(Error **errp) void qmp_guest_suspend_ram(Error **errp)
{ {
Error *local_err = NULL; Error *local_err = NULL;
GuestSuspendMode *mode = g_malloc(sizeof(GuestSuspendMode)); GuestSuspendMode *mode = g_new(GuestSuspendMode, 1);
*mode = GUEST_SUSPEND_MODE_RAM; *mode = GUEST_SUSPEND_MODE_RAM;
check_suspend_mode(*mode, &local_err); check_suspend_mode(*mode, &local_err);

View File

@ -51,12 +51,12 @@ static void qmp_command_info(QmpCommand *cmd, void *opaque)
GuestAgentCommandInfo *cmd_info; GuestAgentCommandInfo *cmd_info;
GuestAgentCommandInfoList *cmd_info_list; 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->name = g_strdup(qmp_command_name(cmd));
cmd_info->enabled = qmp_command_is_enabled(cmd); cmd_info->enabled = qmp_command_is_enabled(cmd);
cmd_info->success_response = qmp_has_success_response(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->value = cmd_info;
cmd_info_list->next = info->supported_commands; cmd_info_list->next = info->supported_commands;
info->supported_commands = cmd_info_list; 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) 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); info->version = g_strdup(QEMU_VERSION);
qmp_for_each_command(qmp_command_info, info); qmp_for_each_command(qmp_command_info, info);

View File

@ -27,7 +27,7 @@ void ga_command_state_add(GACommandState *cs,
void (*init)(void), void (*init)(void),
void (*cleanup)(void)) void (*cleanup)(void))
{ {
GACommandGroup *cg = g_malloc0(sizeof(GACommandGroup)); GACommandGroup *cg = g_new0(GACommandGroup, 1);
cg->init = init; cg->init = init;
cg->cleanup = cleanup; cg->cleanup = cleanup;
cs->groups = g_slist_append(cs->groups, cg); 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 *ga_command_state_new(void)
{ {
GACommandState *cs = g_malloc0(sizeof(GACommandState)); GACommandState *cs = g_new0(GACommandState, 1);
cs->groups = NULL; cs->groups = NULL;
return cs; return cs;
} }