From patchwork Sun Jan 20 22:17:51 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Philippe Waroquiers X-Patchwork-Id: 31135 Received: (qmail 4874 invoked by alias); 20 Jan 2019 22:18:10 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 4860 invoked by uid 89); 20 Jan 2019 22:18:10 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-27.6 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_LOW, SPF_PASS, TIME_LIMIT_EXCEEDED autolearn=unavailable version=3.3.2 spammy=wished, replace, our, HContent-Transfer-Encoding:8bit X-HELO: mailsec110.isp.belgacom.be Received: from mailsec110.isp.belgacom.be (HELO mailsec110.isp.belgacom.be) (195.238.20.106) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Sun, 20 Jan 2019 22:17:59 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=skynet.be; i=@skynet.be; q=dns/txt; s=securemail; t=1548022680; x=1579558680; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=cATYUZmrG90VWkca2j/HNFEsDWNrc5+Fk2mJc2jGBwU=; b=eJeO/OhiHZow0X5OsMXP25GTWALlzuYa7cUiAT43+SAaj2X/DrsX7+QY nJjMBrIHMO5d+J/RGfcGJmYvic5glg==; Received: from 184.205-67-87.adsl-dyn.isp.belgacom.be (HELO md.home) ([87.67.205.184]) by relay.skynet.be with ESMTP/TLS/DHE-RSA-AES128-GCM-SHA256; 20 Jan 2019 23:17:58 +0100 From: Philippe Waroquiers To: gdb-patches@sourceware.org Cc: Philippe Waroquiers Subject: [RFAv2] Fix leak in event-top.c history expansion Date: Sun, 20 Jan 2019 23:17:51 +0100 Message-Id: <20190120221751.28507-1-philippe.waroquiers@skynet.be> MIME-Version: 1.0 X-IsSubscribed: yes 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 >&&) (event-top.c:753) ... Fix the leak by using an unique_xmalloc_ptr for history_value. gdb/ChangeLog 2019-01-20 Philippe Waroquiers * 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 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 (); } }