This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[csl-arm] Win32 host pexec quoting fix.


The win32 commandline uses somewhat strange quoting rules. The code in 
pex-win32.c on csl-arm-branch was getting this wrong. I couldn't find any 
documentation of what's supposed to happen, so this is based on observed 
behaviour of a winXP system. Comments in the code describe the empirically 
derived rules.

The corresponding code has been rewritten on mainline.  The mainline code 
isn't totally correct, but it's closer than the old csl-arm code.

Tested with i686-mingw32->arm-none-eabi cross.
Applied to csl-arm-branch.

Paul

2005-09-07  Paul Brook  <paul@codesourcery.com>

	* pex-win32.c (argv_to_cmdline): Fix escaping of backslash characters.
Index: libiberty/pex-win32.c
===================================================================
RCS file: /var/cvsroot/gcc-cvs/gcc/libiberty/pex-win32.c,v
retrieving revision 1.3.10.4
diff -u -p -r1.3.10.4 pex-win32.c
--- libiberty/pex-win32.c	16 May 2005 11:14:06 -0000	1.3.10.4
+++ libiberty/pex-win32.c	7 Sep 2005 17:04:53 -0000
@@ -72,21 +72,34 @@ argv_to_cmdline(argv)
   char *cmdline;
   char *p;
   size_t cmdline_len;
-  int i, j;
+  int i, j, k;
 
   cmdline_len = 0;
   for (i = 0; argv[i]; i++)
     {
       /* We quote every last argument.  This simplifies the problem;
-	 we need only escape embedded double-quote and backslash
-	 characters.  */
+	 we need only escape embedded double-quotes and immediately
+	 preceeding backslash characters.  A sequence of backslach characters
+	 that is not follwed by a double quote character will not be
+	 escaped.  */
       for (j = 0; argv[i][j]; j++)
-	if (argv[i][j] == '\\' || argv[i][j] == '"')
-	  cmdline_len++;
+	{
+	  if (argv[i][j] == '"')
+	    {
+	      /* Escape preceeding backslashes.  */
+	      for (k = j - 1; k >= 0 && argv[i][k] == '\\'; k--)
+		cmdline_len++;
+	      /* Escape the qote character.  */
+	      cmdline_len++;
+	    }
+	}
+      /* Trailing backslashes also need to be escaped because they will be
+         followed by the terminating quote.  */
+      for (k = j - 1; k >= 0 && argv[i][k] == '\\'; k--)
+	cmdline_len++;
       cmdline_len += j;
       cmdline_len += 3;  /* for leading and trailing quotes and space */
     }
-
   cmdline = xmalloc (cmdline_len);
   p = cmdline;
   for (i = 0; argv[i]; i++)
@@ -94,10 +107,16 @@ argv_to_cmdline(argv)
       *p++ = '"';
       for (j = 0; argv[i][j]; j++)
 	{
-	  if (argv[i][j] == '\\' || argv[i][j] == '"')
-	    *p++ = '\\';
+	  if (argv[i][j] == '"')
+	    {
+	      for (k = j - 1; k >= 0 && argv[i][k] == '\\'; k--)
+		*p++ = '\\';
+	      *p++ = '\\';
+	    }
 	  *p++ = argv[i][j];
 	}
+      for (k = j - 1; k >= 0 && argv[i][k] == '\\'; k--)
+	*p++ = '\\';
       *p++ = '"';
       *p++ = ' ';
     }

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]