Skip to content

Commit e5dc1a6

Browse files
elmarcobonzini
authored andcommitted
readline: add a free function
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 <[email protected]> Reviewed-by: Dr. David Alan Gilbert <[email protected]> Reviewed-by: Philippe Mathieu-Daudé <[email protected]> Message-Id: <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
1 parent 3547112 commit e5dc1a6

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

include/qemu/readline.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,6 @@ ReadLineState *readline_init(ReadLinePrintfFunc *printf_func,
5959
ReadLineFlushFunc *flush_func,
6060
void *opaque,
6161
ReadLineCompletionFunc *completion_finder);
62+
void readline_free(ReadLineState *rs);
6263

6364
#endif /* READLINE_H */

monitor.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,7 @@ static void monitor_data_destroy(Monitor *mon)
583583
if (monitor_is_qmp(mon)) {
584584
json_message_parser_destroy(&mon->qmp.parser);
585585
}
586-
g_free(mon->rs);
586+
readline_free(mon->rs);
587587
QDECREF(mon->outbuf);
588588
qemu_mutex_destroy(&mon->out_lock);
589589
}

util/readline.c

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -500,12 +500,28 @@ const char *readline_get_history(ReadLineState *rs, unsigned int index)
500500
return rs->history[index];
501501
}
502502

503+
void readline_free(ReadLineState *rs)
504+
{
505+
int i;
506+
507+
if (!rs) {
508+
return;
509+
}
510+
for (i = 0; i < READLINE_MAX_CMDS; i++) {
511+
g_free(rs->history[i]);
512+
}
513+
for (i = 0; i < READLINE_MAX_COMPLETIONS; i++) {
514+
g_free(rs->completions[i]);
515+
}
516+
g_free(rs);
517+
}
518+
503519
ReadLineState *readline_init(ReadLinePrintfFunc *printf_func,
504520
ReadLineFlushFunc *flush_func,
505521
void *opaque,
506522
ReadLineCompletionFunc *completion_finder)
507523
{
508-
ReadLineState *rs = g_malloc0(sizeof(*rs));
524+
ReadLineState *rs = g_new0(ReadLineState, 1);
509525

510526
rs->hist_entry = -1;
511527
rs->opaque = opaque;

0 commit comments

Comments
 (0)