From patchwork Sun Aug 6 19:42:31 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 21939 Received: (qmail 105211 invoked by alias); 6 Aug 2017 19:42:45 -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 105180 invoked by uid 89); 6 Aug 2017 19:42:44 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-26.5 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.2 spammy= X-HELO: outbound-ss-1812.hostmonster.com Received: from gproxy1-pub.mail.unifiedlayer.com (HELO outbound-ss-1812.hostmonster.com) (69.89.25.95) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Sun, 06 Aug 2017 19:42:42 +0000 Received: from cmgw2 (cmgw3 [10.0.90.83]) by gproxy1.mail.unifiedlayer.com (Postfix) with ESMTP id EB99B175C42 for ; Sun, 6 Aug 2017 13:42:40 -0600 (MDT) Received: from box522.bluehost.com ([74.220.219.122]) by cmgw2 with id tvid1v00k2f2jeq01vigos; Sun, 06 Aug 2017 13:42:40 -0600 X-Authority-Analysis: v=2.2 cv=Aoae5K1P c=1 sm=1 tr=0 a=GsOEXm/OWkKvwdLVJsfwcA==:117 a=GsOEXm/OWkKvwdLVJsfwcA==:17 a=KeKAF7QvOSUA:10 a=zstS-IiYAAAA:8 a=2BCN0KrDw0p7iPB5WToA:9 a=v5u67HyabOFEmnqn:21 a=cwEXWX3fTJcw32vH:21 a=4G6NA9xxw8l3yy4pmD5M:22 Received: from 75-166-24-97.hlrn.qwest.net ([75.166.24.97]:40168 helo=bapiya.Home) by box522.bluehost.com with esmtpsa (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.87) (envelope-from ) id 1deRRd-0046cM-PN; Sun, 06 Aug 2017 13:42:37 -0600 From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [RFA] C++-ify skip.c Date: Sun, 6 Aug 2017 13:42:31 -0600 Message-Id: <20170806194231.26627-1-tom@tromey.com> X-BWhitelist: no X-Exim-ID: 1deRRd-0046cM-PN X-Source-Sender: 75-166-24-97.hlrn.qwest.net (bapiya.Home) [75.166.24.97]:40168 X-Source-Auth: tom+tromey.com X-Email-Count: 1 X-Source-Cap: ZWx5bnJvYmk7ZWx5bnJvYmk7Ym94NTIyLmJsdWVob3N0LmNvbQ== X-Local-Domain: yes I happened to notice that skiplist_entry, in skip.c, contains a gdb::optional -- but that this object's destructor is never run. This can result in a memory leak. This patch fixes the bug by applying a bit more C++: changing this code to use new and delete, and std::unique_ptr; and removing cleanups in the process. Built and regression tested on x86-64 Fedora 25. ChangeLog 2017-08-06 Tom Tromey * skip.c (skiplist_entry): New constructor. (~skiplist_entry): New destructor. (skiplist_entry::enabled): Now bool. (make_skip_entry): Return a unique_ptr. Use new. (free_skiplist_entry, free_skiplist_entry_cleanup) (make_free_skiplist_entry_cleanup): Remove. (skip_command, skip_disable_command, add_skiplist_entry): Update. (skip_delete_command): Update. Use delete. --- gdb/ChangeLog | 11 ++++++++ gdb/skip.c | 89 +++++++++++++++++++++++------------------------------------ 2 files changed, 45 insertions(+), 55 deletions(-) diff --git a/gdb/ChangeLog b/gdb/ChangeLog index 722fade..12e0d02 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,3 +1,14 @@ +2017-08-06 Tom Tromey + + * skip.c (skiplist_entry): New constructor. + (~skiplist_entry): New destructor. + (skiplist_entry::enabled): Now bool. + (make_skip_entry): Return a unique_ptr. Use new. + (free_skiplist_entry, free_skiplist_entry_cleanup) + (make_free_skiplist_entry_cleanup): Remove. + (skip_command, skip_disable_command, add_skiplist_entry): Update. + (skip_delete_command): Update. Use delete. + 2017-08-05 Tom Tromey * compile/compile-object-load.c (compile_object_load): Use diff --git a/gdb/skip.c b/gdb/skip.c index bf44913..f3291f3 100644 --- a/gdb/skip.c +++ b/gdb/skip.c @@ -38,6 +38,24 @@ struct skiplist_entry { + skiplist_entry (int file_is_glob_, const char *file_, + int function_is_regexp_, const char *function_) + : number (-1), + file_is_glob (file_is_glob_), + file (file_ == NULL ? NULL : xstrdup (file_)), + function_is_regexp (function_is_regexp_), + function (function_ == NULL ? NULL : xstrdup (function_)), + enabled (true), + next (NULL) + { + } + + ~skiplist_entry () + { + xfree (file); + xfree (function); + } + int number; /* Non-zero if FILE is a glob-style pattern. @@ -60,12 +78,12 @@ struct skiplist_entry /* If this is a function regexp, the compiled form. */ gdb::optional compiled_function_regexp; - int enabled; + bool enabled; struct skiplist_entry *next; }; -static void add_skiplist_entry (struct skiplist_entry *e); +static void add_skiplist_entry (std::unique_ptr &&e); static struct skiplist_entry *skiplist_entry_chain; static int skiplist_entry_count; @@ -80,53 +98,18 @@ static int skiplist_entry_count; /* Create a skip object. */ -static struct skiplist_entry * +static std::unique_ptr make_skip_entry (int file_is_glob, const char *file, int function_is_regexp, const char *function) { - struct skiplist_entry *e = XCNEW (struct skiplist_entry); - gdb_assert (file != NULL || function != NULL); if (file_is_glob) gdb_assert (file != NULL); if (function_is_regexp) gdb_assert (function != NULL); - if (file != NULL) - e->file = xstrdup (file); - if (function != NULL) - e->function = xstrdup (function); - e->file_is_glob = file_is_glob; - e->function_is_regexp = function_is_regexp; - e->enabled = 1; - - return e; -} - -/* Free a skiplist entry. */ - -static void -free_skiplist_entry (struct skiplist_entry *e) -{ - xfree (e->file); - xfree (e->function); - xfree (e); -} - -/* Wrapper to free_skiplist_entry for use as a cleanup. */ - -static void -free_skiplist_entry_cleanup (void *e) -{ - free_skiplist_entry ((struct skiplist_entry *) e); -} - -/* Create a cleanup to free skiplist entry E. */ - -static struct cleanup * -make_free_skiplist_entry_cleanup (struct skiplist_entry *e) -{ - return make_cleanup (free_skiplist_entry_cleanup, e); + return std::unique_ptr + (new skiplist_entry (file_is_glob, file, function_is_regexp, function)); } static void @@ -217,7 +200,6 @@ skip_command (char *arg, int from_tty) const char *gfile = NULL; const char *function = NULL; const char *rfunction = NULL; - struct skiplist_entry *e; int i; if (arg == NULL) @@ -291,16 +273,13 @@ skip_command (char *arg, int from_tty) gdb_assert (file != NULL || gfile != NULL || function != NULL || rfunction != NULL); - e = make_skip_entry (gfile != NULL, file ? file : gfile, - rfunction != NULL, function ? function : rfunction); + std::unique_ptr e + = make_skip_entry (gfile != NULL, file ? file : gfile, rfunction != NULL, + function ? function : rfunction); if (rfunction != NULL) - { - struct cleanup *rf_cleanups = make_free_skiplist_entry_cleanup (e); + compile_skip_regexp (e.get (), _("regexp")); - compile_skip_regexp (e, _("regexp")); - discard_cleanups (rf_cleanups); - } - add_skiplist_entry (e); + add_skiplist_entry (std::move (e)); /* I18N concerns drive some of the choices here (we can't piece together the output too much). OTOH we want to keep this simple. Therefore the @@ -414,7 +393,7 @@ skip_enable_command (char *arg, int from_tty) ALL_SKIPLIST_ENTRIES (e) if (arg == NULL || number_is_in_list (arg, e->number)) { - e->enabled = 1; + e->enabled = true; found = 1; } @@ -431,7 +410,7 @@ skip_disable_command (char *arg, int from_tty) ALL_SKIPLIST_ENTRIES (e) if (arg == NULL || number_is_in_list (arg, e->number)) { - e->enabled = 0; + e->enabled = false; found = 1; } @@ -454,7 +433,7 @@ skip_delete_command (char *arg, int from_tty) else skiplist_entry_chain = e->next; - free_skiplist_entry (e); + delete e; found = 1; } else @@ -469,7 +448,7 @@ skip_delete_command (char *arg, int from_tty) /* Add the given skiplist entry to our list, and set the entry's number. */ static void -add_skiplist_entry (struct skiplist_entry *e) +add_skiplist_entry (std::unique_ptr &&e) { struct skiplist_entry *e1; @@ -480,12 +459,12 @@ add_skiplist_entry (struct skiplist_entry *e) e1 = skiplist_entry_chain; if (e1 == NULL) - skiplist_entry_chain = e; + skiplist_entry_chain = e.release (); else { while (e1->next) e1 = e1->next; - e1->next = e; + e1->next = e.release (); } }