Skip to contentSkip to conversation

Trivial memory allocation fixes & cleanups

Qemu-devel Superseded Markus Armbruster 4 participants 22 messages Jan 16, 2013 – Jan 22, 2013

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

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
#1 lore raw

PATCH v2 1/11 g_malloc(0) and g_malloc0(0) return NULL; simplify

Once 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
index 6a95aa6..bc1784c 100644
@@ -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
index 021abaa..8dfefdb 100644
@@ -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
index 0aaf0d2..b795839 100644
@@ -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
index cee8aad..0dd2a9a 100644
@@ -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
#2 lore raw

PATCH v2 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
index 6f427df..6eab7f7 100644
@@ -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
index 0f1b64e..d21ec3a 100644
@@ -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
index 02b5458..0ff5262 100644
@@ -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
index e93b578..0e6e232 100644
@@ -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
index f532b76..c12e724 100644
@@ -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
#3 lore raw

PATCH v2 3/11 hw/9pfs: Fix unchecked strdup() by converting to g_strdup()

In reply to Markus Armbruster
---
 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
index 6eab7f7..74155fb 100644
@@ -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
index 1136021..f1b1c83 100644
@@ -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
#4 lore raw

Re: [Qemu-devel] [PATCH v2 03/11] hw/9pfs: Fix unchecked strdup() by converting to g_strdup()

hw/9pfs/virtio-9p-device.c VirtIODevice *virtio_9p_init(DeviceState *dev, V9fsConf *conf) · line 100

Quoted 13 lines · Markus Armbruster
hw/9pfs/virtio-9p-device.c +1 -1
index 6eab7f7..74155fb 100644
@@ -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;

s->tag is leaked. Want to send a follow-up patch to g_free() it?

#5 lore raw

Re: [Qemu-devel] [PATCH v2 03/11] hw/9pfs: Fix unchecked strdup() by converting to g_strdup()

Quoted 9 lines · Stefan Hajnoczi
Quoted 8 lines · Markus Armbruster
hw/9pfs/virtio-9p-device.c +0 -0
index 6eab7f7..74155fb 100644
@@ -94,7 +94,7 @@ VirtIODevice *virtio_9p_init(DeviceState *dev, V9fsConf *conf)
         exit(1);
     }
 
Quoted 7 lines

hw/9pfs/virtio-9p-device.c VirtIODevice *virtio_9p_init(DeviceState *dev, V9fsConf *conf) · line 100

Quoted 5 lines
hw/9pfs/virtio-9p-device.c +1 -1
-    s->tag = strdup(conf->tag);
+    s->tag = g_strdup(conf->tag);
     s->ctx.uid = -1;
 
     s->ops = fse->ops;

s->tag is leaked. Want to send a follow-up patch to g_free() it?

I'll give it a try.

#6 lore raw

Re: [Qemu-trivial] [Qemu-devel] [PATCH v2 03/11] hw/9pfs: Fix unchecked strdup() by converting to g_strdup()

Quoted 15 lines · Markus Armbruster
Quoted 13 lines · Stefan Hajnoczi
Quoted 12 lines · Markus Armbruster
hw/9pfs/virtio-9p-device.c +1 -1
index 6eab7f7..74155fb 100644
@@ -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;
 
Quoted 5 lines
Quoted 3 lines

hw/9pfs/virtio-9p-device.c VirtIODevice *virtio_9p_init(DeviceState *dev, V9fsConf *conf) · line 100

Quoted 1 line
hw/9pfs/virtio-9p-device.c +0 -0
     s->ops = fse->ops;

s->tag is leaked. Want to send a follow-up patch to g_free() it?

I'll give it a try.

Mind if I wait for Fred Konrad's virtio refactoring to settle? It should make the fix easier, and avoid getting into his way with pointless conflicts.

#7 lore raw

Re: [Qemu-trivial] [Qemu-devel] [PATCH v2 03/11] hw/9pfs: Fix unchecked strdup() by converting to g_strdup()

Quoted 17 lines · Markus Armbruster
Quoted 15 lines · Markus Armbruster
Quoted 13 lines · Stefan Hajnoczi
Quoted 12 lines · Markus Armbruster
hw/9pfs/virtio-9p-device.c +1 -1
index 6eab7f7..74155fb 100644
@@ -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;
 
Quoted 6 lines
Quoted 3 lines
Quoted 2 lines

hw/9pfs/virtio-9p-device.c VirtIODevice *virtio_9p_init(DeviceState *dev, V9fsConf *conf) · line 100

Quoted 1 line
hw/9pfs/virtio-9p-device.c +0 -0
      s->ops = fse->ops;

s->tag is leaked. Want to send a follow-up patch to g_free() it?

I'll give it a try.

Mind if I wait for Fred Konrad's virtio refactoring to settle? It should make the fix easier, and avoid getting into his way with pointless conflicts.

Hi,

Maybe it's due to missing exit function?

Do you want me to add an exit function to g_free this conf->tag?

Fred

#8 lore raw

Re: [Qemu-trivial] [Qemu-devel] [PATCH v2 03/11] hw/9pfs: Fix unchecked strdup() by converting to g_strdup()

Quoted 19 lines · KONRAD Frédéric
Quoted 18 lines · Markus Armbruster
Quoted 16 lines · Markus Armbruster
Quoted 13 lines · Stefan Hajnoczi

hw/9pfs/virtio-9p-device.c VirtIODevice *virtio_9p_init(DeviceState *dev, V9fsConf *conf) · line 99

Quoted 11 lines · Markus Armbruster
hw/9pfs/virtio-9p-device.c +1 -0
index 6eab7f7..74155fb 100644
@@ -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;

s->tag is leaked. Want to send a follow-up patch to g_free() it?

I'll give it a try.

Quoted 8 lines
Quoted 3 lines

Mind if I wait for Fred Konrad's virtio refactoring to settle? It should make the fix easier, and avoid getting into his way with pointless conflicts.

Hi,

Maybe it's due to missing exit function?

Do you want me to add an exit function to g_free this conf->tag?

Yes, please!

#9 lore raw

PATCH v2 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
index a0c9638..d6e04d4 100644
@@ -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
#10 lore raw

PATCH v2 5/11 spice: Fix unchecked strdup() by converting to g_strdup()

In reply to Markus Armbruster
---
 ui/spice-core.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
ui/spice-core.c +2 -2
index 3f2c565..bcc4199 100644
@@ -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
#11 lore raw

PATCH v2 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
index 6b9afd3..36f6616 100644
@@ -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
#12 lore raw

Re: [Qemu-devel] [Qemu-trivial] [PATCH v2 06/11] virtfs-proxy-helper: Fix unchecked strdup() by conv. to g_strdup()

Quoted 20 lines · Markus Armbruster
---
 fsdev/virtfs-proxy-helper.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
fsdev/virtfs-proxy-helper.c +1 -1
index 6b9afd3..36f6616 100644
@@ -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;

fsdev/virtfs-proxy-helper.c int main(int argc, char **argv) · line 1051

Quoted 5 lines
fsdev/virtfs-proxy-helper.c +1 -1
@@ -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);

rpath and sock_name are leaked. Not important though because they are in main().

Stefan

#13 lore raw

Re: [Qemu-devel] [Qemu-trivial] [PATCH v2 06/11] virtfs-proxy-helper: Fix unchecked strdup() by conv. to g_strdup()

Quoted 21 lines · Stefan Hajnoczi
Quoted 20 lines · Markus Armbruster
---
 fsdev/virtfs-proxy-helper.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
fsdev/virtfs-proxy-helper.c +1 -1
index 6b9afd3..36f6616 100644
@@ -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)
Quoted 8 lines

fsdev/virtfs-proxy-helper.c int main(int argc, char **argv) · line 1051

Quoted 5 lines
fsdev/virtfs-proxy-helper.c +1 -1
             sock = atoi(optarg);
             break;
         case 's':
-            sock_name = strdup(optarg);
+            sock_name = g_strdup(optarg);

rpath and sock_name are leaked. Not important though because they are in main().

Yup. Freeing stuff before main returns isn't worth your while or mine :)

#14 lore raw

PATCH v2 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
index b655b30..64a1b88 100644
@@ -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
#15 lore raw

PATCH v2 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
index 64a1b88..30c8b01 100644
@@ -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 const 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((void *)logfilename);
     logfilename = g_strdup(filename);
     if (qemu_logfile) {
         fclose(qemu_logfile);
Signature · 1 line
1.7.11.7
Reviewed-by on v2 8/11
#16 lore raw

Re: [Qemu-devel] [PATCH v2 08/11] qemu-log: Plug trivial memory leak in cpu_set_log_filename()

Quoted 4 lines · Markus Armbruster
---
 qemu-log.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#17 lore raw

Re: [Qemu-devel] [Qemu-trivial] [PATCH v2 08/11] qemu-log: Plug trivial memory leak in cpu_set_log_filename()

In reply to Markus Armbruster
Quoted 16 lines · Markus Armbruster
qemu-log.c +4 -2
index 64a1b88..30c8b01 100644
@@ -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 const char *logfilename;

[...]

qemu-log.c void qemu_set_log(int log_flags, bool use_own_buffers) · line 91

Quoted 5 lines
qemu-log.c +1 -0
@@ -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((void *)logfilename);

Might as well drop the const from the variable declaration to indicate that we allocate/free this string.

Stefan

#18 lore raw

Re: [Qemu-devel] [Qemu-trivial] [PATCH v2 08/11] qemu-log: Plug trivial memory leak in cpu_set_log_filename()

Quoted 18 lines · Stefan Hajnoczi
Quoted 16 lines · Markus Armbruster
qemu-log.c +4 -2
index 64a1b88..30c8b01 100644
@@ -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 const char *logfilename;

[...]

Quoted 8 lines

qemu-log.c void qemu_set_log(int log_flags, bool use_own_buffers) · line 91

Quoted 5 lines
qemu-log.c +1 -0
@@ -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((void *)logfilename);

Might as well drop the const from the variable declaration to indicate that we allocate/free this string.

Never thought of const that way. Dropping const from logfilename is fine with me.

I wish free()'s parameter was const void *. Can't be changed now. Sad that glib didn't get it right either.

#19 lore raw

PATCH v2 9/11 libcacard: Fix unchecked strdup() by converting to g_strdup()

In reply to Markus Armbruster

Note 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
index 5f565e0..df79476 100644
@@ -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
index 313349b..f3efc27 100644
@@ -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
index 2fce52b..9b744f2 100644
@@ -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
#20 lore raw

PATCH v2 10/11 qapi: Fix unchecked strdup() by converting to g_strdup()

In reply to Markus Armbruster

Note 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
index 70cdbca..28bbbe8 100644
@@ -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
#21 lore raw

PATCH v2 11/11 qemu-ga: Fix unchecked strdup() by converting to g_strdup()

In reply to Markus Armbruster

I 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
index 7ffb35e..528b082 100644
@@ -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