objcopy memory leaks after errors

Message ID ZH/kqvEBzDihSIXm@squeak.grove.modra.org
State Accepted
Headers
Series objcopy memory leaks after errors |

Checks

Context Check Description
snail/binutils-gdb-check success Github commit url

Commit Message

Alan Modra June 7, 2023, 2 a.m. UTC
  These aren't important at all, but tidy them in case they obscure
other more important leaks.

	* objcopy (copy_file): Close input bfd after errors.
  

Patch

diff --git a/binutils/objcopy.c b/binutils/objcopy.c
index ac40da87018..414007780a8 100644
--- a/binutils/objcopy.c
+++ b/binutils/objcopy.c
@@ -3821,6 +3821,8 @@  copy_file (const char *input_filename, const char *output_filename, int ofd,
   if (ibfd == NULL || bfd_stat (ibfd, in_stat) != 0)
     {
       bfd_nonfatal_message (input_filename, NULL, NULL, NULL);
+      if (ibfd != NULL)
+	bfd_close (ibfd);
       status = 1;
       return;
     }
@@ -3890,6 +3892,7 @@  copy_file (const char *input_filename, const char *output_filename, int ofd,
 	{
 	  close (ofd);
 	  bfd_nonfatal_message (output_filename, NULL, NULL, NULL);
+	  bfd_close (ibfd);
 	  status = 1;
 	  return;
 	}
@@ -3922,6 +3925,7 @@  copy_file (const char *input_filename, const char *output_filename, int ofd,
  	{
 	  close (ofd);
  	  bfd_nonfatal_message (output_filename, NULL, NULL, NULL);
+	  bfd_close (ibfd);
  	  status = 1;
  	  return;
  	}
@@ -3970,6 +3974,7 @@  copy_file (const char *input_filename, const char *output_filename, int ofd,
       if (core_error == bfd_error_file_ambiguously_recognized)
 	list_matching_formats (core_matching);
 
+      bfd_close (ibfd);
       status = 1;
     }
 }