public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
From: Alan Modra <amodra@gmail.com>
To: binutils@sourceware.org
Subject: PR29925, Memory leak in find_abstract_instance
Date: Thu, 22 Dec 2022 07:58:16 +1030	[thread overview]
Message-ID: <Y6N6cNwCN8q5hhHc@squeak.grove.modra.org> (raw)

The testcase in the PR had a variable with both DW_AT_decl_file and
DW_AT_specification, where the DW_AT_specification also specified
DW_AT_decl_file.  This leads to a memory leak as the file name is
malloced and duplicates are not expected.

I've also changed find_abstract_instance to not use a temp for "name",
because that can result in a change in behaviour from the usual last
of duplicate attributes wins.

	PR 29925
	* dwarf2.c (find_abstract_instance): Delete "name" variable.
	Free *filename_ptr before assigning new file name.
	(scan_unit_for_symbols): Similarly free func->file and
	var->file before assigning.

diff --git a/bfd/dwarf2.c b/bfd/dwarf2.c
index 0cd8152ee6e..b608afbc0cf 100644
--- a/bfd/dwarf2.c
+++ b/bfd/dwarf2.c
@@ -3441,7 +3441,6 @@ find_abstract_instance (struct comp_unit *unit,
   struct abbrev_info *abbrev;
   uint64_t die_ref = attr_ptr->u.val;
   struct attribute attr;
-  const char *name = NULL;
 
   if (recur_count == 100)
     {
@@ -3602,9 +3601,9 @@ find_abstract_instance (struct comp_unit *unit,
 		case DW_AT_name:
 		  /* Prefer DW_AT_MIPS_linkage_name or DW_AT_linkage_name
 		     over DW_AT_name.  */
-		  if (name == NULL && is_str_form (&attr))
+		  if (*pname == NULL && is_str_form (&attr))
 		    {
-		      name = attr.u.str;
+		      *pname = attr.u.str;
 		      if (mangle_style (unit->lang) == 0)
 			*is_linkage = true;
 		    }
@@ -3612,7 +3611,7 @@ find_abstract_instance (struct comp_unit *unit,
 		case DW_AT_specification:
 		  if (is_int_form (&attr)
 		      && !find_abstract_instance (unit, &attr, recur_count + 1,
-						  &name, is_linkage,
+						  pname, is_linkage,
 						  filename_ptr, linenumber_ptr))
 		    return false;
 		  break;
@@ -3622,7 +3621,7 @@ find_abstract_instance (struct comp_unit *unit,
 		     non-string forms into these attributes.  */
 		  if (is_str_form (&attr))
 		    {
-		      name = attr.u.str;
+		      *pname = attr.u.str;
 		      *is_linkage = true;
 		    }
 		  break;
@@ -3630,8 +3629,11 @@ find_abstract_instance (struct comp_unit *unit,
 		  if (!comp_unit_maybe_decode_line_info (unit))
 		    return false;
 		  if (is_int_form (&attr))
-		    *filename_ptr = concat_filename (unit->line_table,
-						     attr.u.val);
+		    {
+		      free (*filename_ptr);
+		      *filename_ptr = concat_filename (unit->line_table,
+						       attr.u.val);
+		    }
 		  break;
 		case DW_AT_decl_line:
 		  if (is_int_form (&attr))
@@ -3643,7 +3645,6 @@ find_abstract_instance (struct comp_unit *unit,
 	    }
 	}
     }
-  *pname = name;
   return true;
 }
 
@@ -4139,8 +4140,11 @@ scan_unit_for_symbols (struct comp_unit *unit)
 
 		case DW_AT_decl_file:
 		  if (is_int_form (&attr))
-		    func->file = concat_filename (unit->line_table,
-						  attr.u.val);
+		    {
+		      free (func->file);
+		      func->file = concat_filename (unit->line_table,
+						    attr.u.val);
+		    }
 		  break;
 
 		case DW_AT_decl_line:
@@ -4182,8 +4186,11 @@ scan_unit_for_symbols (struct comp_unit *unit)
 
 		case DW_AT_decl_file:
 		  if (is_int_form (&attr))
-		    var->file = concat_filename (unit->line_table,
-						 attr.u.val);
+		    {
+		      free (var->file);
+		      var->file = concat_filename (unit->line_table,
+						   attr.u.val);
+		    }
 		  break;
 
 		case DW_AT_decl_line:

-- 
Alan Modra
Australia Development Lab, IBM

                 reply	other threads:[~2022-12-21 21:28 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Y6N6cNwCN8q5hhHc@squeak.grove.modra.org \
    --to=amodra@gmail.com \
    --cc=binutils@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).