[PATCH] gas: Refuse to run if the output file is the same as any of the input files.

John Darrington john@darrington.wattle.id.au
Sun Apr 8 06:20:00 GMT 2018


gas/ChangeLog:
        * as.c (main): Add test to check if the output file is the same
        as any of the input files and if so, exit with an error.
---
 gas/ChangeLog |  5 +++++
 gas/as.c      | 19 +++++++++++++++++++
 2 files changed, 24 insertions(+)

diff --git a/gas/ChangeLog b/gas/ChangeLog
index f7dc5e5cc1..b9276ed156 100644
--- a/gas/ChangeLog
+++ b/gas/ChangeLog
@@ -1,3 +1,8 @@
+2018-04-08  John Darrington <john@darrington.wattle.id.au>
+
+        * as.c (main): Add test to check if the output file is the same
+        as any of the input files and if so, exit with an error.
+
 2018-04-05  H.J. Lu  <hongjiu.lu@intel.com>
 
 	PR gas/22318
diff --git a/gas/as.c b/gas/as.c
index f6da1b1a1d..3ddcee7299 100644
--- a/gas/as.c
+++ b/gas/as.c
@@ -1218,6 +1218,25 @@ main (int argc, char ** argv)
   /* Call parse_args before any of the init/begin functions
      so that switches like --hash-size can be honored.  */
   parse_args (&argc, &argv);
+  struct stat sob;
+  if (0 == stat (out_file_name, &sob))
+    {
+      int i;
+      for (i = 1; i < argc; ++i)
+	{
+	  struct stat sib;
+
+	  if (0 == stat (argv[i], &sib))
+	    {
+	      if (sib.st_ino == sob.st_ino)
+		{
+		  /* Don't let as_fatal remove the output file! */
+		  out_file_name = NULL;
+		  as_fatal (_("The input and output files must be distinct"));
+		}
+	    }
+	}
+    }
   symbol_begin ();
   frag_init ();
   subsegs_begin ();
-- 
2.11.0



More information about the Binutils mailing list