Trivial memory allocation fixes & cleanups
Subsystems (2)
v3 cover Trivial memory allocation fixes & cleanups
v3: const tweak suggested by stefanha in PATCH 08/11, rest identical
except 03/11's commit message mentions leak (thanks stefanha!)
v2: Windows compile fix in PATCH 08/11 (thanks eblake!), rest identical
Markus Armbruster (11):
g_malloc(0) and g_malloc0(0) return NULL; simplify g_strdup(NULL) returns NULL; simplify
hw/9pfs: Fix unchecked strdup() by converting to g_strdup()
readline: Fix unchecked strdup() by converting to g_strdup()
spice: Fix unchecked strdup() by converting to g_strdup()
virtfs-proxy-helper: Fix unchecked strdup() by conv. to g_strdup()
qemu-log: Fix unchecked strdup() by converting to g_strdup()
qemu-log: Plug trivial memory leak in cpu_set_log_filename()
libcacard: Fix unchecked strdup() by converting to g_strdup()
qapi: Fix unchecked strdup() by converting to g_strdup()
qemu-ga: Fix unchecked strdup() by converting to g_strdup()
block/qcow2-refcount.c | 6 +----- block/vdi.c | 4 +--- fsdev/virtfs-proxy-helper.c | 4 ++-- hw/9pfs/virtio-9p-device.c | 8 ++------ hw/9pfs/virtio-9p-local.c | 8 ++++---- hw/9pfs/virtio-9p.c | 6 +----- hw/block-common.c | 4 +--- hw/vhost.c | 7 ++----- libcacard/vcard_emul_nss.c | 4 ++-- libcacard/vreader.c | 2 +- libcacard/vscclient.c | 4 ++-- net/net.c | 4 +--- qapi/qmp-registry.c | 2 +- qemu-log.c | 15 ++++++++++----- qga/commands.c | 2 +- readline.c | 4 ++-- slirp/slirp.c | 8 ++------ ui/spice-core.c | 4 ++-- util/qemu-option.c | 8 ++------ 19 files changed, 40 insertions(+), 64 deletions(-)
Signature · 1 line
1.7.11.7
PATCH v3 5/11 spice: Fix unchecked strdup() by converting to g_strdup()
--- ui/spice-core.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
ui/spice-core.c +2 -2
@@ -848,8 +848,8 @@ static int qemu_spice_set_ticket(bool fail_if_conn, bool disconnect_if_conn)int qemu_spice_set_passwd (const char *passwd,bool fail_if_conn,bool disconnect_if_conn) { -free (auth_passwd); - auth_passwd =strdup (passwd); +g_free (auth_passwd); + auth_passwd =g_strdup (passwd);return qemu_spice_set_ticket (fail_if_conn, disconnect_if_conn); }
Signature · 1 line
1.7.11.7
PATCH v3 6/11 virtfs-proxy-helper: Fix unchecked strdup() by conv. to g_strdup()
In reply to Markus Armbruster--- fsdev/virtfs-proxy-helper.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
fsdev/virtfs-proxy-helper.c +2 -2
@@ -1039,7 +1039,7 @@ int main(int argc, char **argv) }switch (c) {case 'p' : - rpath =strdup (optarg); + rpath =g_strdup (optarg);break ;case 'n' : is_daemon = false;
@@ -1048,7 +1048,7 @@ int main(int argc, char **argv) sock =atoi (optarg);break ;case 's' : - sock_name =strdup (optarg); + sock_name =g_strdup (optarg);break ;case 'u' : own_u =atoi (optarg);
Signature · 1 line
1.7.11.7
PATCH v3 8/11 qemu-log: Plug trivial memory leak in cpu_set_log_filename()
In reply to Markus Armbruster--- qemu-log.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-)
qemu-log.c +9 -4
@@ -21,10 +21,12 @@#include "qemu/log.h" #ifdef WIN32 -static const char *logfilename ="qemu.log" ; +#define DEFAULT_LOGFILENAME "qemu.log"#else -static const char *logfilename ="/tmp/qemu.log" ; +#define DEFAULT_LOGFILENAME "/tmp/qemu.log"#endif + +static char *logfilename;FILE *qemu_logfile;int qemu_loglevel;static int log_append =0 ;
@@ -54,11 +56,13 @@ void qemu_log_mask(int mask, const char *fmt, ...)/* enable or disable low levels log */ void qemu_set_log (int log_flags,bool use_own_buffers) { +const char *fname = logfilename ?:DEFAULT_LOGFILENAME ; + qemu_loglevel = log_flags;if (qemu_loglevel && !qemu_logfile) { - qemu_logfile =fopen (logfilename, log_append ?"a" :"w" ); + qemu_logfile =fopen (fname, log_append ?"a" :"w" );if (!qemu_logfile) { -perror (logfilename); +perror (fname);_exit (1 ); }/* must avoid mmap() usage of glibc by setting a buffer "by hand" */
@@ -84,6 +88,7 @@ void qemu_set_log(int log_flags, bool use_own_buffers)void cpu_set_log_filename (const char *filename) { +g_free (logfilename); logfilename =g_strdup (filename);if (qemu_logfile) {fclose (qemu_logfile);
Signature · 1 line
1.7.11.7
PATCH v3 10/11 qapi: Fix unchecked strdup() by converting to g_strdup()
In reply to Markus ArmbrusterNote that we already free with g_free().
--- qapi/qmp-registry.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
qapi/qmp-registry.c +1 -1
@@ -92,7 +92,7 @@ char **qmp_get_command_list(void) list_head = list =g_malloc0 (count *sizeof (char *)); QTAILQ_FOREACH(cmd, &qmp_commands, node) { - *list =strdup (cmd->name); + *list =g_strdup (cmd->name); list++; }
Signature · 1 line
1.7.11.7
PATCH v3 2/11 g_strdup(NULL) returns NULL; simplify
In reply to Markus Armbruster--- hw/9pfs/virtio-9p-device.c | 6 +----- hw/block-common.c | 4 +--- net/net.c | 4 +--- slirp/slirp.c | 8 ++------ util/qemu-option.c | 8 ++------ 5 files changed, 7 insertions(+), 23 deletions(-)
hw/9pfs/virtio-9p-device.c +1 -5
@@ -85,11 +85,7 @@ VirtIODevice *virtio_9p_init(DeviceState *dev, V9fsConf *conf) } s->ctx.export_flags = fse->export_flags; -if (fse->path) { - s->ctx.fs_root =g_strdup (fse->path); - }else { - s->ctx.fs_root =NULL ; - } + s->ctx.fs_root =g_strdup (fse->path); s->ctx.exops.get_st_gen =NULL ; len =strlen (conf->tag);if (len >MAX_TAG_LEN -1 ) {
hw/block-common.c +1 -3
@@ -18,9 +18,7 @@ void blkconf_serial(BlockConf *conf, char **serial)if (!*serial) {/* try to fall back to value set with legacy -drive serial=... */ dinfo =drive_get_by_blockdev (conf->bs); -if (dinfo->serial) { - *serial =g_strdup (dinfo->serial); - } + *serial =g_strdup (dinfo->serial); } }
net/net.c +1 -3
@@ -566,9 +566,7 @@ static int net_init_nic(const NetClientOptions *opts, const char *name,assert (peer); nd->netdev = peer; } -if (name) { - nd->name =g_strdup (name); - } + nd->name =g_strdup (name);if (nic->has_model) { nd->model =g_strdup (nic->model); }
slirp/slirp.c +2 -6
@@ -225,12 +225,8 @@ Slirp *slirp_init(int restricted, struct in_addr vnetwork,pstrcpy (slirp->client_hostname,sizeof (slirp->client_hostname), vhostname); } -if (tftp_path) { - slirp->tftp_prefix =g_strdup (tftp_path); - } -if (bootfile) { - slirp->bootp_filename =g_strdup (bootfile); - } + slirp->tftp_prefix =g_strdup (tftp_path); + slirp->bootp_filename =g_strdup (bootfile); slirp->vdhcp_startaddr = vdhcp_start; slirp->vnameserver_addr = vnameserver;
util/qemu-option.c +2 -6
@@ -643,9 +643,7 @@ static void opt_set(QemuOpts *opts, const char *name, const char *value,QTAILQ_INSERT_TAIL (&opts->head, opt, next); } opt->desc = desc; -if (value) { - opt->str =g_strdup (value); - } + opt->str =g_strdup (value);qemu_opt_parse (opt, &local_err);if (error_is_set (&local_err)) {error_propagate (errp, local_err);
@@ -792,9 +790,7 @@ QemuOpts *qemu_opts_create(QemuOptsList *list, const char *id, } } opts =g_malloc0 (sizeof (*opts)); -if (id) { - opts->id =g_strdup (id); - } + opts->id =g_strdup (id); opts->list = list;loc_save (&opts->loc);QTAILQ_INIT (&opts->head);
Signature · 1 line
1.7.11.7
PATCH v3 9/11 libcacard: Fix unchecked strdup() by converting to g_strdup()
In reply to Markus ArmbrusterNote that we already free with g_free().
--- libcacard/vcard_emul_nss.c | 4 ++-- libcacard/vreader.c | 2 +- libcacard/vscclient.c | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-)
libcacard/vcard_emul_nss.c +2 -2
@@ -454,7 +454,7 @@ vreader_emul_new(PK11SlotInfo *slot, VCardEmulType type, const char *params) new_reader_emul->slot =PK11_ReferenceSlot (slot); new_reader_emul->default_type = type; - new_reader_emul->type_params =strdup (params); + new_reader_emul->type_params =g_strdup (params); new_reader_emul->present =PR_FALSE ; new_reader_emul->series =0 ; new_reader_emul->saved_vcard =NULL ;
@@ -997,7 +997,7 @@ vcard_emul_init(const VCardEmulOptions *options)/* We should control this with options. For now we mirror out any * removable hardware slot */ default_card_type = options->hw_card_type; - default_type_params =strdup (options->hw_type_params); + default_type_params =g_strdup (options->hw_type_params);SECMOD_GetReadLock (module_lock);for (mlp = module_list; mlp; mlp = mlp->next) {
libcacard/vreader.c +1 -1
@@ -49,7 +49,7 @@ vreader_new(const char *name, VReaderEmul *private, reader = (VReader *)g_malloc (sizeof (VReader));qemu_mutex_init (&reader->lock); reader->reference_count =1 ; - reader->name = name ?strdup (name) :NULL ; + reader->name =g_strdup (name); reader->card =NULL ; reader->id = (vreader_id_t)-1 ; reader->reader_private = private;
libcacard/vscclient.c +2 -2
@@ -503,8 +503,8 @@ main( command_line_options =vcard_emul_options (emul_args); } - qemu_host =strdup (argv[argc -2 ]); - qemu_port =strdup (argv[argc -1 ]); + qemu_host =g_strdup (argv[argc -2 ]); + qemu_port =g_strdup (argv[argc -1 ]); sock =connect_to_qemu (qemu_host, qemu_port);if (sock ==-1 ) {fprintf (stderr,"error opening socket, exiting.\n" );
Signature · 1 line
1.7.11.7
PATCH v3 1/11 g_malloc(0) and g_malloc0(0) return NULL; simplify
In reply to Markus ArmbrusterOnce upon a time, it was decided that qemu_malloc(0) should abort. Switching to glib retired that bright idea. Some code that was added to cope with it (e.g. in commits 702ef63, b76b6e9) is still around. Bury it.
--- block/qcow2-refcount.c | 6 +----- block/vdi.c | 4 +--- hw/9pfs/virtio-9p.c | 6 +----- hw/vhost.c | 7 ++----- 4 files changed, 5 insertions(+), 18 deletions(-)
block/qcow2-refcount.c +1 -5
@@ -737,11 +737,7 @@ int qcow2_update_snapshot_refcount(BlockDriverState *bs,* l1_table_offset when it is the current s->l1_table_offset! Be careful * when changing this! */ if (l1_table_offset != s->l1_table_offset) { -if (l1_size2 !=0 ) { - l1_table =g_malloc0 (align_offset (l1_size2,512 )); - }else { - l1_table =NULL ; - } + l1_table =g_malloc0 (align_offset (l1_size2,512 )); l1_allocated =1 ;if (bdrv_pread (bs->file, l1_table_offset, l1_table, l1_size2) != l1_size2)
block/vdi.c +1 -3
@@ -429,9 +429,7 @@ static int vdi_open(BlockDriverState *bs, int flags) bmap_size = header.blocks_in_image *sizeof (uint32_t ); bmap_size = (bmap_size +SECTOR_SIZE -1 ) /SECTOR_SIZE ; -if (bmap_size >0 ) { - s->bmap =g_malloc (bmap_size *SECTOR_SIZE ); - } + s->bmap =g_malloc (bmap_size *SECTOR_SIZE );if (bdrv_read (bs->file, s->bmap_sector, (uint8_t *)s->bmap, bmap_size) <0 ) { goto fail_free_bmap; }
hw/9pfs/virtio-9p.c +1 -5
@@ -3101,11 +3101,7 @@ static void v9fs_xattrcreate(void *opaque) xattr_fidp->fs.xattr.flags = flags;v9fs_string_init (&xattr_fidp->fs.xattr.name);v9fs_string_copy (&xattr_fidp->fs.xattr.name, &name); -if (size) { - xattr_fidp->fs.xattr.value =g_malloc (size); - }else { - xattr_fidp->fs.xattr.value =NULL ; - } + xattr_fidp->fs.xattr.value =g_malloc (size); err = offset;put_fid (pdu, file_fidp); out_nofid:
hw/vhost.c +2 -5
@@ -269,11 +269,8 @@ static inline void vhost_dev_log_resize(struct vhost_dev* dev, uint64_t size)vhost_log_chunk_t *log;uint64_t log_base;int r, i; -if (size) { - log =g_malloc0 (size *sizeof *log); - }else { - log =NULL ; - } + + log =g_malloc0 (size *sizeof *log); log_base = (uint64_t )(unsigned long )log; r =ioctl (dev->control,VHOST_SET_LOG_BASE , &log_base);assert (r >=0 );
Signature · 1 line
1.7.11.7
PATCH v3 4/11 readline: Fix unchecked strdup() by converting to g_strdup()
In reply to Markus Armbruster--- readline.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
readline.c +2 -2
@@ -247,14 +247,14 @@ static void readline_hist_add(ReadLineState *rs, const char *cmdline) }if (idx ==READLINE_MAX_CMDS ) {/* Need to get one free slot */ -free (rs->history[0 ]); +g_free (rs->history[0 ]);memmove (rs->history, &rs->history[1 ], (READLINE_MAX_CMDS -1 ) *sizeof (char *)); rs->history[READLINE_MAX_CMDS -1 ] =NULL ; idx =READLINE_MAX_CMDS -1 ; }if (new_entry ==NULL ) - new_entry =strdup (cmdline); + new_entry =g_strdup (cmdline); rs->history[idx] = new_entry; rs->hist_entry =-1 ; }
Signature · 1 line
1.7.11.7
PATCH v3 3/11 hw/9pfs: Fix unchecked strdup() by converting to g_strdup()
In reply to Markus ArmbrusterNote: the allocation in virtio_9p_init() is still leaked. To be fixed in a followup commit.
--- hw/9pfs/virtio-9p-device.c | 2 +- hw/9pfs/virtio-9p-local.c | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-)
hw/9pfs/virtio-9p-device.c +1 -1
@@ -94,7 +94,7 @@ VirtIODevice *virtio_9p_init(DeviceState *dev, V9fsConf *conf)exit (1 ); } - s->tag =strdup (conf->tag); + s->tag =g_strdup (conf->tag); s->ctx.uid =-1 ; s->ops = fse->ops;
hw/9pfs/virtio-9p-local.c +4 -4
@@ -46,7 +46,7 @@ static const char *local_mapped_attr_path(FsContext *ctx, const char *path, char *buffer) { char *dir_name; -char *tmp_path =strdup (path); +char *tmp_path =g_strdup (path);char *base_name =basename (tmp_path);/* NULL terminate the directory */
@@ -55,7 +55,7 @@ static const char *local_mapped_attr_path(FsContext *ctx,snprintf (buffer,PATH_MAX ,"%s/%s/%s/%s" , ctx->fs_root, dir_name,VIRTFS_META_DIR , base_name); -free (tmp_path); +g_free (tmp_path);return buffer; }
@@ -130,7 +130,7 @@ static int local_create_mapped_attr_dir(FsContext *ctx, const char *path) {int err;char attr_dir[PATH_MAX ]; -char *tmp_path =strdup (path); +char *tmp_path =g_strdup (path);snprintf (attr_dir,PATH_MAX ,"%s/%s/%s" , ctx->fs_root,dirname (tmp_path),VIRTFS_META_DIR );
@@ -139,7 +139,7 @@ static int local_create_mapped_attr_dir(FsContext *ctx, const char *path)if (err <0 && errno ==EEXIST ) { err =0 ; } -free (tmp_path); +g_free (tmp_path);return err; }
Signature · 1 line
1.7.11.7
PATCH v3 11/11 qemu-ga: Fix unchecked strdup() by converting to g_strdup()
In reply to Markus ArmbrusterI figure it's freed somewhere deep down in QAPI, with g_free().
--- qga/commands.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
qga/commands.c +1 -1
@@ -61,7 +61,7 @@ struct GuestAgentInfo *qmp_guest_info(Error **err)while (*cmd_list) { cmd_info =g_malloc0 (sizeof (GuestAgentCommandInfo)); - cmd_info->name =strdup (*cmd_list); + cmd_info->name =g_strdup (*cmd_list); cmd_info->enabled =qmp_command_is_enabled (cmd_info->name); cmd_info_list =g_malloc0 (sizeof (GuestAgentCommandInfoList));
Signature · 1 line
1.7.11.7
PATCH v3 7/11 qemu-log: Fix unchecked strdup() by converting to g_strdup()
In reply to Markus Armbruster--- qemu-log.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
qemu-log.c +1 -1
@@ -84,7 +84,7 @@ void qemu_set_log(int log_flags, bool use_own_buffers)void cpu_set_log_filename (const char *filename) { - logfilename =strdup (filename); + logfilename =g_strdup (filename);if (qemu_logfile) {fclose (qemu_logfile); qemu_logfile =NULL ;
Signature · 1 line
1.7.11.7
Stefan Hajnoczi
AppliedQuoted 30 lines · Markus Armbruster
v3: const tweak suggested by stefanha in PATCH 08/11, rest identical
except 03/11's commit message mentions leak (thanks stefanha!)v2: Windows compile fix in PATCH 08/11 (thanks eblake!), rest identical
Markus Armbruster (11):
g_malloc(0)andg_malloc0(0)return NULL; simplifyg_strdup(NULL)returns NULL; simplify
hw/9pfs: Fix uncheckedstrdup()by converting tog_strdup()
readline: Fix uncheckedstrdup()by converting tog_strdup()
spice: Fix uncheckedstrdup()by converting tog_strdup()
virtfs-proxy-helper: Fix uncheckedstrdup()by conv. tog_strdup()
qemu-log: Fix uncheckedstrdup()by converting tog_strdup()
qemu-log: Plug trivial memory leak incpu_set_log_filename()
libcacard: Fix uncheckedstrdup()by converting tog_strdup()
qapi: Fix uncheckedstrdup()by converting tog_strdup()
qemu-ga: Fix uncheckedstrdup()by converting tog_strdup()block/qcow2-refcount.c | 6 +----- block/vdi.c | 4 +--- fsdev/virtfs-proxy-helper.c | 4 ++-- hw/9pfs/virtio-9p-device.c | 8 ++------ hw/9pfs/virtio-9p-local.c | 8 ++++---- hw/9pfs/virtio-9p.c | 6 +----- hw/block-common.c | 4 +--- hw/vhost.c | 7 ++----- libcacard/vcard_emul_nss.c | 4 ++-- libcacard/vreader.c | 2 +- libcacard/vscclient.c | 4 ++-- net/net.c | 4 +---
Quoted 13 lines
qapi/qmp-registry.c | 2 +- qemu-log.c | 15 ++++++++++----- qga/commands.c | 2 +- readline.c | 4 ++-- slirp/slirp.c | 8 ++------ ui/spice-core.c | 4 ++-- util/qemu-option.c | 8 ++------ 19 files changed, 40 insertions(+), 64 deletions(-)Signature · 1 line
1.7.11.7
Thanks, applied to the trivial patches -next tree:
https://github.com/stefanha/qemu/commits/trivial-patches-next
Stefan
Re: [Qemu-trivial] [Qemu-devel] [PATCH v3 10/11] qapi: Fix unchecked strdup() by converting to g_strdup()
In reply to Markus ArmbrusterQuoted 4 lines · On Tue, 22 Jan 2013 11:08:05 +0100 Markus Armbruster <[email protected]> wrote:
Note that we already free with
g_free().Signed-off-by Markus Armbruster <[email protected]>Reviewed-by Eric Blake <[email protected]>
Quoted 17 lines
--- qapi/qmp-registry.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)qapi/qmp-registry.c +1
-1@@ -92,7 +92,7 @@ char **qmp_get_command_list(void) list_head = list =g_malloc0 (count *sizeof (char *)); QTAILQ_FOREACH(cmd, &qmp_commands, node) { - *list =strdup (cmd->name); + *list =g_strdup (cmd->name); list++; }
Re: [Qemu-trivial] [Qemu-devel] [PATCH v3 11/11] qemu-ga: Fix unchecked strdup() by converting to g_strdup()
In reply to Markus ArmbrusterQuoted 1 line · On Tue, 22 Jan 2013 11:08:06 +0100 Markus Armbruster <[email protected]> wrote:
I figure it's freed somewhere deep down in QAPI, with
g_free().
It is, by qapi_dealloc_type_str().
Quoted 2 lines
Signed-off-by Markus Armbruster <[email protected]>Reviewed-by Eric Blake <[email protected]>
Quoted 17 lines
--- qga/commands.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)qga/commands.c +1
-1@@ -61,7 +61,7 @@ struct GuestAgentInfo *qmp_guest_info(Error **err)while (*cmd_list) { cmd_info =g_malloc0 (sizeof (GuestAgentCommandInfo)); - cmd_info->name =strdup (*cmd_list); + cmd_info->name =g_strdup (*cmd_list); cmd_info->enabled =qmp_command_is_enabled (cmd_info->name); cmd_info_list =g_malloc0 (sizeof (GuestAgentCommandInfoList));
Re: [Qemu-trivial] [Qemu-devel] [PATCH v3 11/11] qemu-ga: Fix unchecked strdup() by converting to g_strdup()
Quoted 11 lines · Luiz Capitulino
Quoted 1 line · On Tue, 22 Jan 2013 11:08:06 +0100 Markus Armbruster <[email protected]> wrote:
I figure it's freed somewhere deep down in QAPI, with
g_free().It is, by
qapi_dealloc_type_str().Quoted 2 lines
Signed-off-by Markus Armbruster <[email protected]>Reviewed-by Eric Blake <[email protected]>Reviewed-by Luiz Capitulino <[email protected]>
Thanks for the reviews, Luiz. Your Reviewed-by: has been added to the 2 commits.
Stefan