public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [RFAv2] Fix leak in event-top.c history expansion
@ 2019-01-20 22:18 Philippe Waroquiers
  2019-01-21 15:28 ` Tom Tromey
  0 siblings, 1 reply; 3+ messages in thread
From: Philippe Waroquiers @ 2019-01-20 22:18 UTC (permalink / raw)
  To: gdb-patches; +Cc: Philippe Waroquiers

E.g. in gdb.base/default.exp, valgrind detects leaks such as
==17663== 1,438 bytes in 101 blocks are definitely lost in loss record 2,804 of 2,884
==17663==    at 0x4C2BE6D: malloc (vg_replace_malloc.c:309)
==17663==    by 0x418A17: xmalloc (common-utils.c:44)
==17663==    by 0x4E6F19C: history_expand (histexpand.c:1061)
==17663==    by 0x4B4490: handle_line_of_input(buffer*, char const*, int, char const*) (event-top.c:685)
==17663==    by 0x4B4562: command_line_handler(std::unique_ptr<char, gdb::xfree_deleter<char> >&&) (event-top.c:753)
...

Fix the leak by using an unique_xmalloc_ptr for history_value.

gdb/ChangeLog
2019-01-20  Philippe Waroquiers  <philippe.waroquiers@skynet.be>

	* event-top.c (handle_line_of_input): use unique_xmalloc_ptr for
	history_value.
---
 gdb/event-top.c | 18 ++++++++----------
 1 file changed, 8 insertions(+), 10 deletions(-)

diff --git a/gdb/event-top.c b/gdb/event-top.c
index 3d3d6275d7..920b47f7e3 100644
--- a/gdb/event-top.c
+++ b/gdb/event-top.c
@@ -679,31 +679,29 @@ handle_line_of_input (struct buffer *cmd_line_buffer,
   /* Do history expansion if that is wished.  */
   if (history_expansion_p && from_tty && input_interactive_p (current_ui))
     {
-      char *history_value;
+      char *cmd_expansion;
       int expanded;
 
-      expanded = history_expand (cmd, &history_value);
+      expanded = history_expand (cmd, &cmd_expansion);
+      gdb::unique_xmalloc_ptr<char> history_value (cmd_expansion);
       if (expanded)
 	{
 	  size_t len;
 
 	  /* Print the changes.  */
-	  printf_unfiltered ("%s\n", history_value);
+	  printf_unfiltered ("%s\n", history_value.get ());
 
 	  /* If there was an error, call this function again.  */
 	  if (expanded < 0)
-	    {
-	      xfree (history_value);
-	      return cmd;
-	    }
+	    return cmd;
 
 	  /* history_expand returns an allocated string.  Just replace
 	     our buffer with it.  */
-	  len = strlen (history_value);
+	  len = strlen (history_value.get ());
 	  xfree (buffer_finish (cmd_line_buffer));
-	  cmd_line_buffer->buffer = history_value;
+	  cmd_line_buffer->buffer = history_value.get ();
 	  cmd_line_buffer->buffer_size = len + 1;
-	  cmd = history_value;
+	  cmd = history_value.release ();
 	}
     }
 
-- 
2.20.1

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [RFAv2] Fix leak in event-top.c history expansion
  2019-01-20 22:18 [RFAv2] Fix leak in event-top.c history expansion Philippe Waroquiers
@ 2019-01-21 15:28 ` Tom Tromey
  2019-01-21 20:32   ` Philippe Waroquiers
  0 siblings, 1 reply; 3+ messages in thread
From: Tom Tromey @ 2019-01-21 15:28 UTC (permalink / raw)
  To: Philippe Waroquiers; +Cc: gdb-patches

>>>>> "Philippe" == Philippe Waroquiers <philippe.waroquiers@skynet.be> writes:

Philippe> gdb/ChangeLog
Philippe> 2019-01-20  Philippe Waroquiers  <philippe.waroquiers@skynet.be>

Philippe> 	* event-top.c (handle_line_of_input): use unique_xmalloc_ptr for
Philippe> 	history_value.

Thanks, this is ok.

Tom

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [RFAv2] Fix leak in event-top.c history expansion
  2019-01-21 15:28 ` Tom Tromey
@ 2019-01-21 20:32   ` Philippe Waroquiers
  0 siblings, 0 replies; 3+ messages in thread
From: Philippe Waroquiers @ 2019-01-21 20:32 UTC (permalink / raw)
  To: Tom Tromey; +Cc: gdb-patches

On Mon, 2019-01-21 at 08:28 -0700, Tom Tromey wrote:
> > > > > > "Philippe" == Philippe Waroquiers <philippe.waroquiers@skynet.be> writes:
> 
> Philippe> gdb/ChangeLog
> Philippe> 2019-01-20  Philippe Waroquiers  <philippe.waroquiers@skynet.be>
> 
> Philippe> 	* event-top.c (handle_line_of_input): use unique_xmalloc_ptr for
> Philippe> 	history_value.
> 
> Thanks, this is ok.
Pushed, thanks for the review.
Philippe

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2019-01-21 20:32 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-01-20 22:18 [RFAv2] Fix leak in event-top.c history expansion Philippe Waroquiers
2019-01-21 15:28 ` Tom Tromey
2019-01-21 20:32   ` Philippe Waroquiers

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 read-only IMAP folder(s) and NNTP newsgroup(s).