public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
From: Alan Modra <amodra@gmail.com>
To: binutils@sourceware.org
Subject: Re: PR31145, potential memory leak in binutils/ld
Date: Wed, 20 Dec 2023 08:44:22 +1030	[thread overview]
Message-ID: <ZYIVvlMnwKEyTNsE@squeak.grove.modra.org> (raw)
In-Reply-To: <ZXvEvjFRF4xFNj1+@squeak.grove.modra.org>

Revert most of this patch, it isn't correct to free the BFD_IN_MEMORY
iostream in io_reinit.

	PR 31145
	* format.c (io_reinit): Revert last change.  Comment.
	* opncls.c (_bfd_delete_bfd): Likewise.

diff --git a/bfd/format.c b/bfd/format.c
index 31aeb52861a..0790a3a2ee5 100644
--- a/bfd/format.c
+++ b/bfd/format.c
@@ -163,16 +163,15 @@ io_reinit (bfd *abfd, struct bfd_preserve *preserve)
   if (abfd->iovec != preserve->iovec)
     {
       /* Handle file backed to in-memory transition.  bfd_cache_close
-	 won't do anything unless abfd->iovec is the cache_iovec.  */
+	 won't do anything unless abfd->iovec is the cache_iovec.
+	 Don't be tempted to call iovec->bclose here.  We don't want
+	 to call memory_bclose, which would free the bim.  The bim
+	 must be kept if bfd_check_format_matches is going to decide
+	 later that the PE format needing it is in fact the correct
+	 target match.  */
       bfd_cache_close (abfd);
       abfd->iovec = preserve->iovec;
-
-      if (abfd->iostream != preserve->iostream)
-	{
-	  if ((abfd->flags & BFD_IN_MEMORY) != 0)
-	    free (abfd->iostream);
-	  abfd->iostream = preserve->iostream;
-	}
+      abfd->iostream = preserve->iostream;
 
       /* Handle in-memory to file backed transition.  */
       if ((abfd->flags & BFD_CLOSED_BY_CACHE) != 0
diff --git a/bfd/opncls.c b/bfd/opncls.c
index e7b39593608..32c95d6ba52 100644
--- a/bfd/opncls.c
+++ b/bfd/opncls.c
@@ -176,8 +176,6 @@ _bfd_delete_bfd (bfd *abfd)
   else
     free ((char *) bfd_get_filename (abfd));
 
-  if ((abfd->flags & BFD_IN_MEMORY) != 0)
-    free (abfd->iostream);
   free (abfd->arelt_data);
   free (abfd);
 }

-- 
Alan Modra
Australia Development Lab, IBM

      parent reply	other threads:[~2023-12-19 22:14 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-15  3:15 Alan Modra
2023-12-15  9:04 ` Torbjorn SVENSSON
2023-12-15 12:05   ` Alan Modra
2023-12-19 22:14 ` Alan Modra [this message]

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=ZYIVvlMnwKEyTNsE@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).