From patchwork Sun Dec 1 19:10:45 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Philippe Waroquiers X-Patchwork-Id: 36406 Received: (qmail 84470 invoked by alias); 1 Dec 2019 19:10: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 84455 invoked by uid 89); 1 Dec 2019 19:10:55 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-22.3 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_STOCKGEN, RCVD_IN_DNSWL_LOW, SPF_PASS autolearn=ham version=3.3.1 spammy=slots, releasing, sk:SYMBOL_, xfree 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, 01 Dec 2019 19:10:52 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=skynet.be; i=@skynet.be; q=dns/txt; s=securemail; t=1575227453; x=1606763453; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=BFlB6zLgoCHiRlm8H6EItMr6dlc+RTbzYdzjmo7kFCU=; b=qctuZotcBH54cVr6ia40PSlESV39Ps1EgYitgQxDoL2gnjggXWJIHOHz +1aMX/OwQ4w6lEe54yKyVVsnAKeeBA==; IronPort-SDR: vO9q1Lw/ytlWjYyzOAnVnzEVTEXIhIuGrzQAWQPqDnbxG2EpfHT9KKi2rc7AU442ZqLoR1J6xj 4OfxqmzTUkZHGLv7/bL7AGSTz4o6meXatwPAuHY5mRMTGxt1n8u/GGa31TzZMiYm7bber8d6N0 fAXCMP47SIG/o94sImyvCt60ogY6VRPWW3xcePdLm4Abzo9JSRJUxoR6DS5WB17lE/uXxTrImp WzDLkFpvGswmx7IGfkryj/QaqvxZPD6jvUZDUPSU49DJaWrb/i16fTQCZOr9SAuL+IyrEx1Oao hDk= Received: from 136.173-134-109.adsl-dyn.isp.belgacom.be (HELO md.home) ([109.134.173.136]) by relay.skynet.be with ESMTP/TLS/ECDHE-RSA-AES128-GCM-SHA256; 01 Dec 2019 20:10:51 +0100 From: Philippe Waroquiers To: gdb-patches@sourceware.org Cc: Philippe Waroquiers Subject: [RFA] Fix leak of symbol name in block_symbol_cache Date: Sun, 1 Dec 2019 20:10:45 +0100 Message-Id: <20191201191045.1256-1-philippe.waroquiers@skynet.be> MIME-Version: 1.0 X-IsSubscribed: yes A symbol not found inserted in the cache has a xstrdup-ed name that must be freed, but only the struct block_symbol_cache is freed. Add a function destroy_block_symbol_cache that clears all slots before releasing the cache. YYYY-MM-DD Philippe Waroquiers * symtab.c (symbol_cache_clear_slot): Move close to cleared type. (destroy_block_symbol_cache): New function. (symbol_cache:~symbol_cache) Call destroy_block_symbol_cache. (resize_symbol_cache): Likewise. --- gdb/symtab.c | 41 +++++++++++++++++++++++++++-------------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/gdb/symtab.c b/gdb/symtab.c index 894a323003..5c33fbf9ab 100644 --- a/gdb/symtab.c +++ b/gdb/symtab.c @@ -183,6 +183,16 @@ struct symbol_cache_slot } value; }; +/* Clear out SLOT. */ + +static void +symbol_cache_clear_slot (struct symbol_cache_slot *slot) +{ + if (slot->state == SYMBOL_SLOT_NOT_FOUND) + xfree (slot->value.not_found.name); + slot->state = SYMBOL_SLOT_UNUSED; +} + /* Symbols don't specify global vs static block. So keep them in separate caches. */ @@ -201,6 +211,19 @@ struct block_symbol_cache struct symbol_cache_slot symbols[1]; }; +/* Clear all slots of BSC and free BSC. */ + +static void +destroy_block_symbol_cache (struct block_symbol_cache *bsc) +{ + if (bsc != nullptr) + { + for (unsigned int i = 0; i < bsc->size; i++) + symbol_cache_clear_slot (&bsc->symbols[i]); + xfree (bsc); + } +} + /* The symbol cache. Searching for symbols in the static and global blocks over multiple objfiles @@ -217,8 +240,8 @@ struct symbol_cache ~symbol_cache () { - xfree (global_symbols); - xfree (static_symbols); + destroy_block_symbol_cache (global_symbols); + destroy_block_symbol_cache (static_symbols); } struct block_symbol_cache *global_symbols = nullptr; @@ -1234,8 +1257,8 @@ resize_symbol_cache (struct symbol_cache *cache, unsigned int new_size) && new_size == 0)) return; - xfree (cache->global_symbols); - xfree (cache->static_symbols); + destroy_block_symbol_cache (cache->global_symbols); + destroy_block_symbol_cache (cache->static_symbols); if (new_size == 0) { @@ -1373,16 +1396,6 @@ symbol_cache_lookup (struct symbol_cache *cache, return {}; } -/* Clear out SLOT. */ - -static void -symbol_cache_clear_slot (struct symbol_cache_slot *slot) -{ - if (slot->state == SYMBOL_SLOT_NOT_FOUND) - xfree (slot->value.not_found.name); - slot->state = SYMBOL_SLOT_UNUSED; -} - /* Mark SYMBOL as found in SLOT. OBJFILE_CONTEXT is the current objfile when the lookup was done, or NULL if it's not needed to distinguish lookups (STATIC_BLOCK). It is *not*