diff mbox

Windows libibery: Don't quote args unnecessarily

Message ID 1399445760-4060-2-git-send-email-mingw.android@gmail.com
State New
Headers show

Commit Message

Ray Donnelly May 7, 2014, 6:56 a.m. UTC
We only quote arguments that contain spaces, \t or "
characters to prevent wasting 2 characters per
argument of the CreateProcess() 32,768 limit.
---
 libiberty/pex-win32.c | 46 +++++++++++++++++++++++++++++++++++++---------
 1 file changed, 37 insertions(+), 9 deletions(-)

Comments

Kai Tietz May 8, 2014, 7:22 a.m. UTC | #1
Ah, that was my mail-client.

Patch is ok IMO.  I add Ian CC for a second look at it.

Cheers,
Kai
Ian Lance Taylor May 9, 2014, 5:08 a.m. UTC | #2
On Tue, May 6, 2014 at 11:56 PM, Ray Donnelly <mingw.android@gmail.com> wrote:
> We only quote arguments that contain spaces, \t or "
> characters to prevent wasting 2 characters per
> argument of the CreateProcess() 32,768 limit.

This is OK.

Thanks.

Ian


>  libiberty/pex-win32.c | 46 +++++++++++++++++++++++++++++++++++++---------
>  1 file changed, 37 insertions(+), 9 deletions(-)
>
> diff --git a/libiberty/pex-win32.c b/libiberty/pex-win32.c
> index eae72c5..8b9d4f0 100644
> --- a/libiberty/pex-win32.c
> +++ b/libiberty/pex-win32.c
> @@ -340,17 +340,25 @@ argv_to_cmdline (char *const *argv)
>    char *p;
>    size_t cmdline_len;
>    int i, j, k;
> +  int needs_quotes;
>
>    cmdline_len = 0;
>    for (i = 0; argv[i]; i++)
>      {
> -      /* We quote every last argument.  This simplifies the problem;
> -        we need only escape embedded double-quotes and immediately
> +      /* We only quote arguments that contain spaces, \t or " characters to
> +        prevent wasting 2 chars per argument of the CreateProcess 32k char
> +        limit.  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.  */
> +      needs_quotes = 0;
>        for (j = 0; argv[i][j]; j++)
>         {
> +         if (argv[i][j] == ' ' || argv[i][j] == '\t' || argv[i][j] == '"')
> +           {
> +             needs_quotes = 1;
> +           }
> +
>           if (argv[i][j] == '"')
>             {
>               /* Escape preceeding backslashes.  */
> @@ -362,16 +370,33 @@ argv_to_cmdline (char *const *argv)
>         }
>        /* 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++;
> +      if (needs_quotes)
> +        {
> +          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 */
> +      /* for leading and trailing quotes and space */
> +      cmdline_len += needs_quotes * 2 + 1;
>      }
>    cmdline = XNEWVEC (char, cmdline_len);
>    p = cmdline;
>    for (i = 0; argv[i]; i++)
>      {
> -      *p++ = '"';
> +      needs_quotes = 0;
> +      for (j = 0; argv[i][j]; j++)
> +        {
> +          if (argv[i][j] == ' ' || argv[i][j] == '\t' || argv[i][j] == '"')
> +            {
> +              needs_quotes = 1;
> +              break;
> +            }
> +        }
> +
> +      if (needs_quotes)
> +        {
> +          *p++ = '"';
> +        }
>        for (j = 0; argv[i][j]; j++)
>         {
>           if (argv[i][j] == '"')
> @@ -382,9 +407,12 @@ argv_to_cmdline (char *const *argv)
>             }
>           *p++ = argv[i][j];
>         }
> -      for (k = j - 1; k >= 0 && argv[i][k] == '\\'; k--)
> -       *p++ = '\\';
> -      *p++ = '"';
> +      if (needs_quotes)
> +        {
> +          for (k = j - 1; k >= 0 && argv[i][k] == '\\'; k--)
> +            *p++ = '\\';
> +          *p++ = '"';
> +        }
>        *p++ = ' ';
>      }
>    p[-1] = '\0';
> --
> 1.9.2
>
diff mbox

Patch

diff --git a/libiberty/pex-win32.c b/libiberty/pex-win32.c
index eae72c5..8b9d4f0 100644
--- a/libiberty/pex-win32.c
+++ b/libiberty/pex-win32.c
@@ -340,17 +340,25 @@  argv_to_cmdline (char *const *argv)
   char *p;
   size_t cmdline_len;
   int i, j, k;
+  int needs_quotes;
 
   cmdline_len = 0;
   for (i = 0; argv[i]; i++)
     {
-      /* We quote every last argument.  This simplifies the problem;
-	 we need only escape embedded double-quotes and immediately
+      /* We only quote arguments that contain spaces, \t or " characters to
+	 prevent wasting 2 chars per argument of the CreateProcess 32k char
+	 limit.  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.  */
+      needs_quotes = 0;
       for (j = 0; argv[i][j]; j++)
 	{
+	  if (argv[i][j] == ' ' || argv[i][j] == '\t' || argv[i][j] == '"')
+	    {
+	      needs_quotes = 1;
+	    }
+
 	  if (argv[i][j] == '"')
 	    {
 	      /* Escape preceeding backslashes.  */
@@ -362,16 +370,33 @@  argv_to_cmdline (char *const *argv)
 	}
       /* 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++;
+      if (needs_quotes)
+        {
+          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 */
+      /* for leading and trailing quotes and space */
+      cmdline_len += needs_quotes * 2 + 1;
     }
   cmdline = XNEWVEC (char, cmdline_len);
   p = cmdline;
   for (i = 0; argv[i]; i++)
     {
-      *p++ = '"';
+      needs_quotes = 0;
+      for (j = 0; argv[i][j]; j++)
+        {
+          if (argv[i][j] == ' ' || argv[i][j] == '\t' || argv[i][j] == '"')
+            {
+              needs_quotes = 1;
+              break;
+            }
+        }
+
+      if (needs_quotes)
+        {
+          *p++ = '"';
+        }
       for (j = 0; argv[i][j]; j++)
 	{
 	  if (argv[i][j] == '"')
@@ -382,9 +407,12 @@  argv_to_cmdline (char *const *argv)
 	    }
 	  *p++ = argv[i][j];
 	}
-      for (k = j - 1; k >= 0 && argv[i][k] == '\\'; k--)
-	*p++ = '\\';
-      *p++ = '"';
+      if (needs_quotes)
+        {
+          for (k = j - 1; k >= 0 && argv[i][k] == '\\'; k--)
+            *p++ = '\\';
+          *p++ = '"';
+        }
       *p++ = ' ';
     }
   p[-1] = '\0';