From patchwork Mon Dec 31 08:19:39 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Philippe Waroquiers X-Patchwork-Id: 30908 Received: (qmail 73857 invoked by alias); 31 Dec 2018 08:19:55 -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 73848 invoked by uid 89); 31 Dec 2018 08:19:55 -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 autolearn=ham version=3.3.2 spammy=machines X-HELO: mailsec103.isp.belgacom.be Received: from mailsec103.isp.belgacom.be (HELO mailsec103.isp.belgacom.be) (195.238.20.99) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Mon, 31 Dec 2018 08:19:52 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=skynet.be; i=@skynet.be; q=dns/txt; s=securemail; t=1546244392; x=1577780392; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=skwZZDRyh2DKpA1fD3fTPbAQpMIRHkLZ5s4RORr/xs8=; b=imXU1BM/jIS/jb7VCkjZiHLg3OYC65ztdaBZeBxfcLGPIBTdMYGOqxiE HDQcGTJYnEWhX2uQ32u49xqFsxw2sQ==; 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; 31 Dec 2018 09:19:50 +0100 From: Philippe Waroquiers To: gdb-patches@sourceware.org Cc: Philippe Waroquiers Subject: [RFA] Use gdb::unique_xmalloc_ptr in command_line_input to fix a leak Date: Mon, 31 Dec 2018 09:19:39 +0100 Message-Id: <20181231081939.9180-1-philippe.waroquiers@skynet.be> MIME-Version: 1.0 X-IsSubscribed: yes Following the change of logic where the input_handler gets a gdb::unique_xmalloc_ptr, a call to readline directly followed by a call to handle_line_of_input is missing a free, and causes the below leak. Use gdb::unique_xmalloc_ptr to solve the leak. ==16291== VALGRIND_GDB_ERROR_BEGIN ==16291== 64 bytes in 1 blocks are definitely lost in loss record 1,815 of 4,111 ==16291== at 0x4C2E2B3: realloc (vg_replace_malloc.c:836) ==16291== by 0x41EB1C: xrealloc (common-utils.c:62) ==16291== by 0x41DBD3: buffer_grow(buffer*, char const*, unsigned long) [clone .part.1] (buffer.c:40) ==16291== by 0x66E8FF: buffer_grow_char (buffer.h:40) ==16291== by 0x66E8FF: gdb_readline_no_editing (top.c:798) ==16291== by 0x66E8FF: command_line_input(char const*, char const*) (top.c:1249) ==16291== by 0x66EBD8: read_command_file(_IO_FILE*) (top.c:421) ==16291== by 0x412C0C: script_from_file(_IO_FILE*, char const*) (cli-script.c:1547) ==16291== by 0x40BE90: source_script_from_stream (cli-cmds.c:569) ==16291== by 0x40BE90: source_script_with_search(char const*, int, int) (cli-cmds.c:606) ==16291== by 0x54D567: catch_command_errors(void (*)(char const*, int), char const*, int) (main.c:379) ==16291== by 0x54EA84: captured_main_1 (main.c:994) ==16291== by 0x54EA84: captured_main (main.c:1167) ==16291== by 0x54EA84: gdb_main(captured_main_args*) (main.c:1193) ==16291== by 0x29DA27: main (gdb.c:32) ==16291== ==16291== VALGRIND_GDB_ERROR_END gdb/ChangeLog 2018-12-31 Philippe Waroquiers * top.c (command_line_input): Use unique_xmalloc_ptr to manage memory allocated by readline. --- gdb/top.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/gdb/top.c b/gdb/top.c index 8b3fc5ee9a..17e78841ae 100644 --- a/gdb/top.c +++ b/gdb/top.c @@ -1212,7 +1212,7 @@ command_line_input (const char *prompt_arg, const char *annotation_suffix) while (1) { - char *rl; + gdb::unique_xmalloc_ptr rl; /* Make sure that all output has been output. Some machines may let you get away with leaving out some of the gdb_flush, but @@ -1236,20 +1236,20 @@ command_line_input (const char *prompt_arg, const char *annotation_suffix) && from_tty && input_interactive_p (current_ui)) { - rl = (*deprecated_readline_hook) (prompt); + rl.reset ((*deprecated_readline_hook) (prompt)); } else if (command_editing_p && from_tty && input_interactive_p (current_ui)) { - rl = gdb_readline_wrapper (prompt); + rl.reset (gdb_readline_wrapper (prompt)); } else { - rl = gdb_readline_no_editing (prompt); + rl.reset (gdb_readline_no_editing (prompt)); } - cmd = handle_line_of_input (&cmd_line_buffer, rl, + cmd = handle_line_of_input (&cmd_line_buffer, rl.get (), 0, annotation_suffix); if (cmd == (char *) EOF) {