From patchwork Fri Aug 11 21:18:46 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 22106 Received: (qmail 57957 invoked by alias); 11 Aug 2017 21:19:03 -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 57936 invoked by uid 89); 11 Aug 2017 21:19:02 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-25.4 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, RCVD_IN_SORBS_SPAM, SPF_PASS autolearn=ham version=3.3.2 spammy=H*m:8455, leaks, H*MI:8455 X-HELO: gproxy6-pub.mail.unifiedlayer.com Received: from gproxy6-pub.mail.unifiedlayer.com (HELO gproxy6-pub.mail.unifiedlayer.com) (67.222.39.168) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Fri, 11 Aug 2017 21:19:00 +0000 Received: from cmgw2 (unknown [10.0.90.83]) by gproxy6.mail.unifiedlayer.com (Postfix) with ESMTP id 938D41E08D4 for ; Fri, 11 Aug 2017 15:18:58 -0600 (MDT) Received: from box522.bluehost.com ([74.220.219.122]) by cmgw2 with id vxJv1v00g2f2jeq01xJyHP; Fri, 11 Aug 2017 15:18:58 -0600 X-Authority-Analysis: v=2.2 cv=T7z8d7CQ c=1 sm=1 tr=0 a=GsOEXm/OWkKvwdLVJsfwcA==:117 a=GsOEXm/OWkKvwdLVJsfwcA==:17 a=KeKAF7QvOSUA:10 a=zstS-IiYAAAA:8 a=20KFwNOVAAAA:8 a=M1XPo70-sU4MNOmpHlYA:9 a=+jEqtf1s3R9VXZ0wqowq2kgwd+I=:19 a=-nyFquofbs32EhZw:21 a=IhvhP9qw-nkFcRox:21 a=4G6NA9xxw8l3yy4pmD5M:22 Received: from 75-166-24-97.hlrn.qwest.net ([75.166.24.97]:50162 helo=bapiya.Home) by box522.bluehost.com with esmtpsa (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.87) (envelope-from ) id 1dgHKZ-002ZZX-9Q; Fri, 11 Aug 2017 15:18:55 -0600 From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [RFA] Fix memory leak in add_symbol_file_command Date: Fri, 11 Aug 2017 15:18:46 -0600 Message-Id: <20170811211846.8455-1-tom@tromey.com> X-BWhitelist: no X-Exim-ID: 1dgHKZ-002ZZX-9Q X-Source-Sender: 75-166-24-97.hlrn.qwest.net (bapiya.Home) [75.166.24.97]:50162 X-Source-Auth: tom+tromey.com X-Email-Count: 1 X-Source-Cap: ZWx5bnJvYmk7ZWx5bnJvYmk7Ym94NTIyLmJsdWVob3N0LmNvbQ== X-Local-Domain: yes I happened to notice that add_symbol_file_command leaks "sect_opts". This patch fixes the leak by changing sect_opts to be a std::vector. I had to change the logic in the loop a little bit. Previously, it was incrementing section_index after completing an entry; but this changes it to push a new entry when the name is seen. I believe the argument parsing here is mildly incorrect, in that nothing checks whether the -s option actually had any arguments. Maybe gdb can crash if "-s NAME" is given without an argument. I didn't try to fix this in this patch, but I do have another patch I can send later that fixes it up. Regression tested on the buildbot. ChangeLog 2017-08-11 Tom Tromey * symfile.c (add_symbol_file_command): Use std::vector. --- gdb/ChangeLog | 4 ++++ gdb/symfile.c | 43 +++++++++++-------------------------------- 2 files changed, 15 insertions(+), 32 deletions(-) diff --git a/gdb/ChangeLog b/gdb/ChangeLog index c588291..20f6d60 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,3 +1,7 @@ +2017-08-11 Tom Tromey + + * symfile.c (add_symbol_file_command): Use std::vector. + 2017-08-11 Pedro Alves * infrun.c (process_event_stop_test): Adjust diff --git a/gdb/symfile.c b/gdb/symfile.c index 3e2df9b..4158dd4 100644 --- a/gdb/symfile.c +++ b/gdb/symfile.c @@ -2205,10 +2205,8 @@ add_symbol_file_command (char *args, int from_tty) struct gdbarch *gdbarch = get_current_arch (); gdb::unique_xmalloc_ptr filename; char *arg; - int section_index = 0; int argcnt = 0; int sec_num = 0; - int i; int expecting_sec_name = 0; int expecting_sec_addr = 0; struct objfile *objf; @@ -2225,13 +2223,9 @@ add_symbol_file_command (char *args, int from_tty) }; struct section_addr_info *section_addrs; - struct sect_opt *sect_opts = NULL; - size_t num_sect_opts = 0; + std::vector sect_opts; struct cleanup *my_cleanups = make_cleanup (null_cleanup, NULL); - num_sect_opts = 16; - sect_opts = XNEWVEC (struct sect_opt, num_sect_opts); - dont_repeat (); if (args == NULL) @@ -2251,16 +2245,8 @@ add_symbol_file_command (char *args, int from_tty) { /* The second argument is always the text address at which to load the program. */ - sect_opts[section_index].name = ".text"; - sect_opts[section_index].value = arg; - if (++section_index >= num_sect_opts) - { - num_sect_opts *= 2; - sect_opts = ((struct sect_opt *) - xrealloc (sect_opts, - num_sect_opts - * sizeof (struct sect_opt))); - } + struct sect_opt sect = { ".text", arg }; + sect_opts.push_back (sect); } else { @@ -2268,21 +2254,14 @@ add_symbol_file_command (char *args, int from_tty) to an option. */ if (expecting_sec_name) { - sect_opts[section_index].name = arg; + struct sect_opt sect = { arg, NULL }; + sect_opts.push_back (sect); expecting_sec_name = 0; } else if (expecting_sec_addr) { - sect_opts[section_index].value = arg; + sect_opts.back ().value = arg; expecting_sec_addr = 0; - if (++section_index >= num_sect_opts) - { - num_sect_opts *= 2; - sect_opts = ((struct sect_opt *) - xrealloc (sect_opts, - num_sect_opts - * sizeof (struct sect_opt))); - } } else if (strcmp (arg, "-readnow") == 0) flags |= OBJF_READNOW; @@ -2301,7 +2280,7 @@ add_symbol_file_command (char *args, int from_tty) filename, and the second is the address where this file has been loaded. Abort now if this address hasn't been provided by the user. */ - if (section_index < 1) + if (sect_opts.size () < 1) error (_("The address where %s has been loaded is missing"), filename.get ()); @@ -2313,13 +2292,13 @@ add_symbol_file_command (char *args, int from_tty) printf_unfiltered (_("add symbol table from file \"%s\" at\n"), filename.get ()); - section_addrs = alloc_section_addr_info (section_index); + section_addrs = alloc_section_addr_info (sect_opts.size ()); make_cleanup (xfree, section_addrs); - for (i = 0; i < section_index; i++) + for (sect_opt § : sect_opts) { CORE_ADDR addr; - const char *val = sect_opts[i].value; - const char *sec = sect_opts[i].name; + const char *val = sect.value; + const char *sec = sect.name; addr = parse_and_eval_address (val);