[patch] Fix memory leak on bfd_close(BFD_IN_MEMORY)

Jan Kratochvil jan.kratochvil@redhat.com
Tue Aug 1 16:07:00 GMT 2006


Hi,

I feel the patch
2004-10-24  Daniel Jacobowitz  <dan@debian.org>

	* opncls.c (bfd_close): Return TRUE for BFD_IN_MEMORY.

Index: opncls.c
===================================================================
RCS file: /cvs/src/src/bfd/opncls.c,v
retrieving revision 1.25
retrieving revision 1.26
diff -u -p -r1.25 -r1.26
--- opncls.c	10 Oct 2004 13:58:05 -0000	1.25
+++ opncls.c	24 Oct 2004 18:45:38 -0000	1.26
@@ -598,7 +598,7 @@ bfd_close (bfd *abfd)
   if (!(abfd->flags & BFD_IN_MEMORY))
     ret = abfd->iovec->bclose (abfd);
   else
-    ret = 0;
+    ret = TRUE;
 
   /* If the file was open for writing and is now executable,
      make it so.  */

introduced a memory leak.  This codepath occurs only on ia64 on the second run:
	(gdb) run
	(gdb) run

Attached patch which IMO fixes the leak and it does not crash (although it was
not ElectricFence-d) on ia64.

Maybe not worth it (yes, the proper function set should be implemented etc.).


Regards,
Jan Kratochvil
-------------- next part --------------
https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=200402


diff -ru gdb-6.3-orig/bfd/opncls.c gdb-6.3/bfd/opncls.c
--- gdb-6.3-orig/bfd/opncls.c	2006-07-28 04:56:05.000000000 -0400
+++ gdb-6.3/bfd/opncls.c	2006-07-28 04:56:25.000000000 -0400
@@ -595,27 +595,36 @@
 
   /* FIXME: cagney/2004-02-15: Need to implement a BFD_IN_MEMORY io
      vector.  */
-  if (!(abfd->flags & BFD_IN_MEMORY))
-    ret = abfd->iovec->bclose (abfd);
-  else
-    ret = TRUE;
+  if (abfd->flags & BFD_IN_MEMORY)
+    {
+      struct bfd_in_memory *bim = abfd->iostream;
 
-  /* If the file was open for writing and is now executable,
-     make it so.  */
-  if (ret
-      && abfd->direction == write_direction
-      && abfd->flags & EXEC_P)
+      free (bim->buffer);
+      free (bim);
+
+      ret = TRUE;
+    }
+  else
     {
-      struct stat buf;
+      ret = abfd->iovec->bclose (abfd);
 
-      if (stat (abfd->filename, &buf) == 0)
+      /* If the file was open for writing and is now executable,
+	 make it so.  */
+      if (ret
+	  && abfd->direction == write_direction
+	  && abfd->flags & EXEC_P)
 	{
-	  unsigned int mask = umask (0);
+	  struct stat buf;
 
-	  umask (mask);
-	  chmod (abfd->filename,
-		 (0777
-		  & (buf.st_mode | ((S_IXUSR | S_IXGRP | S_IXOTH) &~ mask))));
+	  if (stat (abfd->filename, &buf) == 0)
+	    {
+	      unsigned int mask = umask (0);
+
+	      umask (mask);
+	      chmod (abfd->filename,
+		     (0777
+		      & (buf.st_mode | ((S_IXUSR | S_IXGRP | S_IXOTH) &~ mask))));
+	    }
 	}
     }
 


More information about the Gdb-patches mailing list