From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pj1-x1029.google.com (mail-pj1-x1029.google.com [IPv6:2607:f8b0:4864:20::1029]) by sourceware.org (Postfix) with ESMTPS id 2D4533858436 for ; Thu, 30 Mar 2023 04:49:34 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 2D4533858436 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-pj1-x1029.google.com with SMTP id x15so16168545pjk.2 for ; Wed, 29 Mar 2023 21:49:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1680151773; h=content-disposition:mime-version:message-id:subject:to:from:date :from:to:cc:subject:date:message-id:reply-to; bh=HFKRzRbgrNVUr3llTOEec4OOq8vK+2cZ85krtz6d/ic=; b=OPAYyjzSIQjRkISIVU9vn1YxLQgZSQHZt39JoKOCl4/XKlrNvl2nvn8MsYfP38w5Dj 4X54/lmB2LYSUeIhP1gH0BETCcKYlzA8lsPQmqk10TB+irxFkXcc0CPN+GhjtH75ZvvH iY079C/UsgKxQFt1j08zmuQzym+fnmKyD/5eRapMBrdEwMgSHKQzvZ438/TmJC2kSNq5 rMgMh4ZCxZPOjouYSW/RoxgYA2J36eyyhGbyerZvi2gqmHS3SZUFGu7K6280cYQXZSaa YOzex5zUG9QQCK206/3dQPin0iuEVHOdLJvuoAQP+qBkfFUNph33g0FycjLJND5MCkUk NW4w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680151773; h=content-disposition:mime-version:message-id:subject:to:from:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=HFKRzRbgrNVUr3llTOEec4OOq8vK+2cZ85krtz6d/ic=; b=XNouROuXZg2FomFCjT/mbFrUnAf3WFrvTHetmdm3F8TpQIRdm4zTS+TTWhmdfhnt8P TiiBzP+OdeQVfVUFw2E7PWSW/IVRfiQb3Azw3qdZUaK7LIHb9GQBlREVj23OauSaMn4U l7qFdNIBQb15M+VWQflKiWPunmF0XvYoht20s0lKrUam80iH4zKBqRcleARNTfFq/kbA ORFu/40ufLqf3wJCfHbQMFK56H4FQmv/uPV3hoFZ/dTW4wbPUwryDhkFQ1KjZrHoN4QV IZuNlms16FFeCgzGfvDaS2Qej/mNYkQJl7RF7vbJYIKVL7W4GV6upoY+lLSLJIeUS15X j6jA== X-Gm-Message-State: AAQBX9cNpt7lqJHFUhCpUwUeEnIAnFQSoi0t1jUJ4wgn3nxFMcJfSC8a 5WxD3a9PBW8D0RRPZgAQDG373WsqYn4= X-Google-Smtp-Source: AKy350Y4xrO3a6dLQNUIcAliyv+a175HdcglcLw0fqJG3aYy3JDRiIJPn7ZK6/tP26KG7WU0+oaPKA== X-Received: by 2002:a05:6a20:7883:b0:df:81de:93dd with SMTP id d3-20020a056a20788300b000df81de93ddmr14221106pzg.44.1680151772763; Wed, 29 Mar 2023 21:49:32 -0700 (PDT) Received: from squeak.grove.modra.org (158.106.96.58.static.exetel.com.au. [58.96.106.158]) by smtp.gmail.com with ESMTPSA id u13-20020aa7838d000000b0059085684b54sm24599294pfm.140.2023.03.29.21.49.31 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 29 Mar 2023 21:49:32 -0700 (PDT) Received: by squeak.grove.modra.org (Postfix, from userid 1000) id F3D631142E2D; Thu, 30 Mar 2023 15:19:29 +1030 (ACDT) Date: Thu, 30 Mar 2023 15:19:29 +1030 From: Alan Modra To: binutils@sourceware.org Subject: Tidy leaked objcopy memory Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Spam-Status: No, score=-3035.1 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: * objcopy.c (delete_symbol_htabs): Also free symbols. (write_debugging_info): Free strings and syms once written. * wrstabs.c (write_stabs_in_sections_debugging_info): memset entire info struct. Free hash tables before returning. Free syms on error return. diff --git a/binutils/objcopy.c b/binutils/objcopy.c index cf830442b3c..72585ea9ed7 100644 --- a/binutils/objcopy.c +++ b/binutils/objcopy.c @@ -1065,6 +1065,10 @@ delete_symbol_htabs (void) htab_delete (weaken_specific_htab); htab_delete (redefine_specific_htab); htab_delete (redefine_specific_reverse_htab); + + free (isympp); + if (osympp != isympp) + free (osympp); } /* Add a symbol to strip_specific_list. */ @@ -4656,6 +4660,7 @@ write_debugging_info (bfd *obfd, void *dhandle, bfd_size_type symsize, stringsize; asection *stabsec, *stabstrsec; flagword flags; + bool ret; if (! write_stabs_in_sections_debugging_info (obfd, dhandle, &syms, &symsize, &strings, @@ -4682,17 +4687,19 @@ write_debugging_info (bfd *obfd, void *dhandle, the next thing the caller is going to do is copy over the real sections. We may someday have to split the contents setting out of this function. */ + ret = true; if (! bfd_set_section_contents (obfd, stabsec, syms, 0, symsize) || ! bfd_set_section_contents (obfd, stabstrsec, strings, 0, stringsize)) { bfd_nonfatal_message (NULL, obfd, NULL, _("can't set debugging section contents")); - free (strings); - return false; + ret = false; } - return true; + free (strings); + free (syms); + return ret; } bfd_nonfatal_message (NULL, obfd, NULL, diff --git a/binutils/wrstabs.c b/binutils/wrstabs.c index 2df2b041434..53ade2eafbd 100644 --- a/binutils/wrstabs.c +++ b/binutils/wrstabs.c @@ -469,16 +469,18 @@ write_stabs_in_sections_debugging_info (bfd *abfd, void *dhandle, struct string_hash_entry *h; bfd_byte *p; + memset (&info, 0, sizeof info); info.abfd = abfd; - info.symbols_size = 0; info.symbols_alloc = 500; info.symbols = (bfd_byte *) xmalloc (info.symbols_alloc); - info.strings = NULL; - info.last_string = NULL; /* Reserve 1 byte for a null byte. */ info.strings_size = 1; + info.type_index = 1; + info.so_offset = -1; + info.fun_offset = -1; + info.pending_lbrac = (bfd_vma) -1; if (!bfd_hash_table_init (&info.strhash.table, string_hash_newfunc, sizeof (struct string_hash_entry)) @@ -487,38 +489,28 @@ write_stabs_in_sections_debugging_info (bfd *abfd, void *dhandle, { non_fatal ("bfd_hash_table_init_failed: %s", bfd_errmsg (bfd_get_error ())); - return false; + goto fail; } - info.type_stack = NULL; - info.type_index = 1; - memset (&info.type_cache, 0, sizeof info.type_cache); - info.so_offset = -1; - info.fun_offset = -1; - info.last_text_address = 0; - info.nesting = 0; - info.fnaddr = 0; - info.pending_lbrac = (bfd_vma) -1; - /* The initial symbol holds the string size. */ if (! stab_write_symbol (&info, 0, 0, 0, (const char *) NULL)) - return false; + goto fail; /* Output an initial N_SO symbol. */ info.so_offset = info.symbols_size; if (! stab_write_symbol (&info, N_SO, 0, 0, bfd_get_filename (abfd))) - return false; + goto fail; if (! debug_write (dhandle, &stab_fns, (void *) &info)) - return false; + goto fail; if (info.pending_lbrac != (bfd_vma) -1) - return false; + goto fail; /* Output a trailing N_SO. */ if (! stab_write_symbol (&info, N_SO, 0, info.last_text_address, (const char *) NULL)) - return false; + goto fail; /* Put the string size in the initial symbol. */ bfd_put_32 (abfd, info.strings_size, info.symbols + 8); @@ -537,7 +529,17 @@ write_stabs_in_sections_debugging_info (bfd *abfd, void *dhandle, p += strlen ((char *) p) + 1; } + bfd_hash_table_free (&info.typedef_hash.table); + bfd_hash_table_free (&info.strhash.table); return true; + + fail: + if (info.typedef_hash.table.memory) + bfd_hash_table_free (&info.typedef_hash.table); + if (info.strhash.table.memory) + bfd_hash_table_free (&info.strhash.table); + free (info.symbols); + return false; } /* Start writing out information for a compilation unit. */ -- Alan Modra Australia Development Lab, IBM