qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: "Marc-André Lureau" <marcandre.lureau@redhat.com>
To: qemu-devel@nongnu.org
Cc: pbonzini@redhat.com, famz@redhat.com, eblake@redhat.com,
	"Marc-André Lureau" <marcandre.lureau@redhat.com>,
	"Dr. David Alan Gilbert" <dgilbert@redhat.com>,
	"Markus Armbruster" <armbru@redhat.com>
Subject: [Qemu-devel] [PATCH v3 10/18] readline: add a free function
Date: Thu,  4 Jan 2018 17:05:15 +0100	[thread overview]
Message-ID: <20180104160523.22995-11-marcandre.lureau@redhat.com> (raw)
In-Reply-To: <20180104160523.22995-1-marcandre.lureau@redhat.com>

Fixes leaks such as:

Direct leak of 2 byte(s) in 1 object(s) allocated from:
    #0 0x7eff58beb850 in malloc (/lib64/libasan.so.4+0xde850)
    #1 0x7eff57942f0c in g_malloc ../glib/gmem.c:94
    #2 0x7eff579431cf in g_malloc_n ../glib/gmem.c:331
    #3 0x7eff5795f6eb in g_strdup ../glib/gstrfuncs.c:363
    #4 0x55db720f1d46 in readline_hist_add /home/elmarco/src/qq/util/readline.c:258
    #5 0x55db720f2d34 in readline_handle_byte /home/elmarco/src/qq/util/readline.c:387
    #6 0x55db71539d00 in monitor_read /home/elmarco/src/qq/monitor.c:3896
    #7 0x55db71f9be35 in qemu_chr_be_write_impl /home/elmarco/src/qq/chardev/char.c:167
    #8 0x55db71f9bed3 in qemu_chr_be_write /home/elmarco/src/qq/chardev/char.c:179
    #9 0x55db71fa013c in fd_chr_read /home/elmarco/src/qq/chardev/char-fd.c:66
    #10 0x55db71fe18a8 in qio_channel_fd_source_dispatch /home/elmarco/src/qq/io/channel-watch.c:84
    #11 0x7eff5793a90b in g_main_dispatch ../glib/gmain.c:3182
    #12 0x7eff5793b7ac in g_main_context_dispatch ../glib/gmain.c:3847
    #13 0x55db720af3bd in glib_pollfds_poll /home/elmarco/src/qq/util/main-loop.c:214
    #14 0x55db720af505 in os_host_main_loop_wait /home/elmarco/src/qq/util/main-loop.c:261
    #15 0x55db720af6d6 in main_loop_wait /home/elmarco/src/qq/util/main-loop.c:515
    #16 0x55db7184e0de in main_loop /home/elmarco/src/qq/vl.c:1995
    #17 0x55db7185e956 in main /home/elmarco/src/qq/vl.c:4914
    #18 0x7eff4ea17039 in __libc_start_main (/lib64/libc.so.6+0x21039)

(while at it, use g_new0(ReadLineState), it's a bit easier to read)

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Reviewed-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
Reviewed-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
---
 include/qemu/readline.h |  1 +
 monitor.c               |  2 +-
 util/readline.c         | 18 +++++++++++++++++-
 3 files changed, 19 insertions(+), 2 deletions(-)

diff --git a/include/qemu/readline.h b/include/qemu/readline.h
index c08cf7400e..e81258322b 100644
--- a/include/qemu/readline.h
+++ b/include/qemu/readline.h
@@ -59,5 +59,6 @@ ReadLineState *readline_init(ReadLinePrintfFunc *printf_func,
                              ReadLineFlushFunc *flush_func,
                              void *opaque,
                              ReadLineCompletionFunc *completion_finder);
+void readline_free(ReadLineState *rs);
 
 #endif /* READLINE_H */
diff --git a/monitor.c b/monitor.c
index d682eee2d8..b9da5e20d1 100644
--- a/monitor.c
+++ b/monitor.c
@@ -583,7 +583,7 @@ static void monitor_data_destroy(Monitor *mon)
     if (monitor_is_qmp(mon)) {
         json_message_parser_destroy(&mon->qmp.parser);
     }
-    g_free(mon->rs);
+    readline_free(mon->rs);
     QDECREF(mon->outbuf);
     qemu_mutex_destroy(&mon->out_lock);
 }
diff --git a/util/readline.c b/util/readline.c
index bbdee790b0..24ec839854 100644
--- a/util/readline.c
+++ b/util/readline.c
@@ -500,12 +500,28 @@ const char *readline_get_history(ReadLineState *rs, unsigned int index)
     return rs->history[index];
 }
 
+void readline_free(ReadLineState *rs)
+{
+    int i;
+
+    if (!rs) {
+        return;
+    }
+    for (i = 0; i < READLINE_MAX_CMDS; i++) {
+        g_free(rs->history[i]);
+    }
+    for (i = 0; i < READLINE_MAX_COMPLETIONS; i++) {
+        g_free(rs->completions[i]);
+    }
+    g_free(rs);
+}
+
 ReadLineState *readline_init(ReadLinePrintfFunc *printf_func,
                              ReadLineFlushFunc *flush_func,
                              void *opaque,
                              ReadLineCompletionFunc *completion_finder)
 {
-    ReadLineState *rs = g_malloc0(sizeof(*rs));
+    ReadLineState *rs = g_new0(ReadLineState, 1);
 
     rs->hist_entry = -1;
     rs->opaque = opaque;
-- 
2.15.1.355.g36791d7216

  parent reply	other threads:[~2018-01-04 16:08 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-01-04 16:05 [Qemu-devel] [PATCH v3 00/18] Various build-sys and sanitizer related fixes Marc-André Lureau
2018-01-04 16:05 ` [Qemu-devel] [PATCH v3 01/18] build-sys: fix qemu-ga -pthread linking Marc-André Lureau
2018-01-04 16:05 ` [Qemu-devel] [PATCH v3 02/18] build-sys: silence make by default or V=0 Marc-André Lureau
2018-01-04 16:05 ` [Qemu-devel] [PATCH v3 03/18] build-sys: add a rule to print a variable Marc-André Lureau
2018-01-04 16:15   ` Philippe Mathieu-Daudé
2018-01-04 16:05 ` [Qemu-devel] [PATCH v3 04/18] build-sys: compile with -Og or -O1 when --enable-debug Marc-André Lureau
2018-01-04 17:17   ` Philippe Mathieu-Daudé
2018-01-04 16:05 ` [Qemu-devel] [PATCH v3 05/18] tests/docker: add some sanitizers to fedora dockerfile Marc-André Lureau
2018-01-04 17:11   ` Philippe Mathieu-Daudé
2018-01-04 16:05 ` [Qemu-devel] [PATCH v3 06/18] tests/docker: add test-debug Marc-André Lureau
2018-01-04 17:16   ` Philippe Mathieu-Daudé
2018-01-04 16:05 ` [Qemu-devel] [PATCH v3 07/18] build-sys: add some sanitizers when --enable-debug if possible Marc-André Lureau
2018-01-04 17:07   ` Philippe Mathieu-Daudé
2018-01-04 16:05 ` [Qemu-devel] [PATCH v3 08/18] tests: fix check-qobject leak Marc-André Lureau
2018-01-04 16:05 ` [Qemu-devel] [PATCH v3 09/18] vl: fix direct firmware directories leak Marc-André Lureau
2018-01-04 16:05 ` Marc-André Lureau [this message]
2018-01-04 16:05 ` [Qemu-devel] [PATCH v3 11/18] tests: fix migration-test leak Marc-André Lureau
2018-01-04 17:28   ` Juan Quintela
2018-01-04 16:05 ` [Qemu-devel] [PATCH v3 12/18] crypto: fix stack-buffer-overflow error Marc-André Lureau
2018-01-04 16:40   ` Thomas Huth
2018-01-04 17:10     ` Philippe Mathieu-Daudé
2018-01-04 16:05 ` [Qemu-devel] [PATCH v3 13/18] qemu-config: fix leak in query-command-line-options Marc-André Lureau
2018-01-04 16:05 ` [Qemu-devel] [PATCH v3 14/18] tests: fix qmp-test leak Marc-André Lureau
2018-01-04 16:05 ` [Qemu-devel] [PATCH v3 15/18] ucontext: annotate coroutine stack for ASAN Marc-André Lureau
2018-01-09 11:09   ` Philippe Mathieu-Daudé
2018-01-09 11:10     ` Philippe Mathieu-Daudé
2018-01-09 11:23     ` Marc-André Lureau
2018-01-12 11:15       ` Marc-André Lureau
2018-01-12 11:21         ` Paolo Bonzini
2018-01-04 16:05 ` [Qemu-devel] [PATCH v3 16/18] tests: fix coroutine leak in /basic/entered Marc-André Lureau
2018-01-04 16:05 ` [Qemu-devel] [PATCH v3 17/18] mips: fix potential fopen(NULL,...) Marc-André Lureau
2018-01-04 16:27   ` [Qemu-devel] [PATCH v3 17/18] mips: fix potential fopen(NULL, ...) Philippe Mathieu-Daudé
2018-01-04 16:05 ` [Qemu-devel] [PATCH v3 18/18] disas/s390: fix global-buffer-overflow Marc-André Lureau
2018-01-04 17:02 ` [Qemu-devel] [PATCH v3 00/18] Various build-sys and sanitizer related fixes no-reply
2018-01-05 10:20 ` Paolo Bonzini

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180104160523.22995-11-marcandre.lureau@redhat.com \
    --to=marcandre.lureau@redhat.com \
    --cc=armbru@redhat.com \
    --cc=dgilbert@redhat.com \
    --cc=eblake@redhat.com \
    --cc=famz@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).