From patchwork Wed Jan 8 01:12:58 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Terekhov, Mikhail via Gdb-patches" X-Patchwork-Id: 37246 Received: (qmail 18313 invoked by alias); 8 Jan 2020 01:13:05 -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 18304 invoked by uid 89); 8 Jan 2020 01:13:05 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-21.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.1 spammy=HX-Languages-Length:2559 X-HELO: mail-qt1-f202.google.com Received: from mail-qt1-f202.google.com (HELO mail-qt1-f202.google.com) (209.85.160.202) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Wed, 08 Jan 2020 01:13:03 +0000 Received: by mail-qt1-f202.google.com with SMTP id m8so1007199qta.20 for ; Tue, 07 Jan 2020 17:13:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:message-id:mime-version:subject:from:to:cc; bh=Wv/QLOgNu2nR5k5CItH1KWc6hFmAYKEt8SxI4fzVCnc=; b=lWhnDZ0wa2SaA0nljL2BmMEJ+JHHnLx9hYijoM4vlqU59bwSmaxNZoWNX4DILcmdsy juTGYJWkBXylbej3dp1veu8BYLy0l63X/5gFsh4ALQ7B+gdCYKZ/ln8TybDX88EZVNRx /N8FBtY6Z0ZLtNRJstjm4p7zghZJ/QJIozeDkg1rBBjBFreyjinJlRwGiFaiD7NLoBc7 4fi6uAq9ne6Lkavj5C1lYinKf0iCvINJwXENtFI6i/B2Cgh1Fuws2zmg29KmGc3hVqQ5 8etXzJN+K0p2Yn57fp9Dge88XoIF7dhUr2HZx0yS/Yl/swxPxIw9W98fFyT3WX2YE8Mc AiJg== Date: Tue, 7 Jan 2020 19:12:58 -0600 Message-Id: <20200108011258.59443-1-cbiesinger@google.com> Mime-Version: 1.0 Subject: [PATCH] Fix memory leak of the demangled symbol name X-Patchwork-Original-From: "Christian Biesinger via gdb-patches" From: "Terekhov, Mikhail via Gdb-patches" Reply-To: Christian Biesinger To: gdb-patches@sourceware.org Cc: Christian Biesinger X-IsSubscribed: yes compute_and_set_names would only free the name if we did not find the name in the hashtable, but it needs to always free it. Solve this by moving the smart pointer outside the if. Thanks to PhilippeW for finding this. gdb/ChangeLog: 2020-01-07 Christian Biesinger * symtab.c (general_symbol_info::compute_and_set_names): Move the unique_xmalloc_ptr outside the if to always free the demangled name. Change-Id: Id7c6b8408432183700ccb5ff634818d6c5a3ac95 --- gdb/symtab.c | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/gdb/symtab.c b/gdb/symtab.c index 88b8faedb5..122f9051af 100644 --- a/gdb/symtab.c +++ b/gdb/symtab.c @@ -892,6 +892,16 @@ general_symbol_info::compute_and_set_names (gdb::string_view linkage_name, htab_find_slot_with_hash (per_bfd->demangled_names_hash.get (), &entry, *hash, INSERT)); + /* The const_cast is safe because the only reason it is already + initialized is if we purposefully set it from a background + thread to avoid doing the work here. However, it is still + allocated from the heap and needs to be freed by us, just + like if we called symbol_find_demangled_name here. If this is + nullptr, we call symbol_find_demangled_name below, but we put + this smart pointer here to be sure that we don't leak this name. */ + gdb::unique_xmalloc_ptr demangled_name + (const_cast (language_specific.demangled_name)); + /* If this name is not in the hash table, add it. */ if (*slot == NULL /* A C version of the symbol may have already snuck into the table. @@ -914,15 +924,9 @@ general_symbol_info::compute_and_set_names (gdb::string_view linkage_name, else linkage_name_copy = linkage_name; - /* The const_cast is safe because the only reason it is already - initialized is if we purposefully set it from a background - thread to avoid doing the work here. However, it is still - allocated from the heap and needs to be freed by us, just - like if we called symbol_find_demangled_name here. */ - gdb::unique_xmalloc_ptr demangled_name - (language_specific.demangled_name - ? const_cast (language_specific.demangled_name) - : symbol_find_demangled_name (this, linkage_name_copy.data ())); + if (demangled_name.get () == nullptr) + demangled_name.reset + (symbol_find_demangled_name (this, linkage_name_copy.data ())); /* Suppose we have demangled_name==NULL, copy_name==0, and linkage_name_copy==linkage_name. In this case, we already have the