[RFA] Fix leak in most gdb.mi tests.

Message ID 20181229170116.8588-1-philippe.waroquiers@skynet.be
State New, archived
Headers

Commit Message

Philippe Waroquiers Dec. 29, 2018, 5:01 p.m. UTC
  Valgrind detects a leak on the input_buffer in most (all?) gdb.mi tests,
e.g. gdb.mi/mi2-var-child.exp:

  ==27567== 23,120 bytes in 313 blocks are definitely lost in loss record 3,149 of 3,200
  ==27567==    at 0x4C2E2B3: realloc (vg_replace_malloc.c:836)
  ==27567==    by 0x403D1C: xrealloc (common-utils.c:62)
  ==27567==    by 0x402DD3: buffer_grow(buffer*, char const*, unsigned long) [clone .part.1] (buffer.c:40)
  ==27567==    by 0x49CECA: buffer_grow_char (buffer.h:40)
  ==27567==    by 0x49CECA: gdb_readline_no_editing_callback(void*) (event-top.c:847)
  ==27567==    by 0x49D27F: stdin_event_handler(int, void*) (event-top.c:511)
  ==27567==    by 0x49C0CC: gdb_wait_for_event(int) (event-loop.c:859)
  ==27567==    by 0x49C203: gdb_do_one_event() [clone .part.4] (event-loop.c:347)
  ==27567==    by 0x49C394: gdb_do_one_event (common-exceptions.h:219)
  ==27567==    by 0x49C394: start_event_loop() (event-loop.c:371)
  ==27567==    by 0x532687: captured_command_loop() (main.c:330)
  ==27567==    by 0x53367C: captured_main (main.c:1177)
  ==27567==    by 0x53367C: gdb_main(captured_main_args*) (main.c:1193)
  ==27567==    by 0x2841A7: main (gdb.c:32)

Fix the leak by freeing in mi_execute_command_input_handler
the command allocated in gdb_readline_no_editing_callback.

Tested on amd64, native and under valgrind.

gdb/ChangeLog
2018-12-29  Philippe Waroquiers  <philippe.waroquiers@skynet.be>

	* mi/mi-interp.c (mi_execute_command_input_handler): xfree cmd,
	allocated in gdb_readline_no_editing_callback.
---
 gdb/mi/mi-interp.c | 3 +++
 1 file changed, 3 insertions(+)
  

Comments

Tom Tromey Dec. 29, 2018, 7:15 p.m. UTC | #1
>>>>> "Philippe" == Philippe Waroquiers <philippe.waroquiers@skynet.be> writes:

Philippe> Fix the leak by freeing in mi_execute_command_input_handler
Philippe> the command allocated in gdb_readline_no_editing_callback.

Philippe> Tested on amd64, native and under valgrind.

Philippe> gdb/ChangeLog
Philippe> 2018-12-29  Philippe Waroquiers  <philippe.waroquiers@skynet.be>

Philippe> 	* mi/mi-interp.c (mi_execute_command_input_handler): xfree cmd,
Philippe> 	allocated in gdb_readline_no_editing_callback.

Thanks.

Would you mind adding to the comment comment above struct ui::input_handler
(in top.h) to explain that input_handler takes ownership of the string?

This all seems somewhat ugly to me.  Perhaps ideally the caller should
manage the memory and input_handler should take a const char *?  But not
your problem, fixing the leak is enough.

This is ok with the comment tweak.

Tom
  
Tom Tromey Dec. 29, 2018, 7:33 p.m. UTC | #2
>>>>> "Tom" == Tom Tromey <tom@tromey.com> writes:

Tom> This all seems somewhat ugly to me.  Perhaps ideally the caller should
Tom> manage the memory and input_handler should take a const char *?  But not
Tom> your problem, fixing the leak is enough.

I poked at this and it isn't trivial due to gdb_readline_wrapper_line.

Maybe input_handler could accept a unique_xmalloc_ptr<char>&& instead.
That would at least clarify the ownership semantics.

Tom
  

Patch

diff --git a/gdb/mi/mi-interp.c b/gdb/mi/mi-interp.c
index 9a317bc0ec..d2a46106a1 100644
--- a/gdb/mi/mi-interp.c
+++ b/gdb/mi/mi-interp.c
@@ -310,6 +310,9 @@  mi_execute_command_input_handler (char *cmd)
      stops.  */
   if (ui->prompt_state == PROMPT_NEEDED)
     display_mi_prompt (mi);
+
+  /* Allocated in gdb_readline_no_editing_callback.  */
+  xfree (cmd);
 }
 
 void