-
Notifications
You must be signed in to change notification settings - Fork 0
/
132-gcc-4.3-dont-escape-arguments-that-dont-need-it-in-pex-win32.c.patch
98 lines (95 loc) · 2.97 KB
/
132-gcc-4.3-dont-escape-arguments-that-dont-need-it-in-pex-win32.c.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
From bd9a3d23bf3e492d092f106b91d21101d59fdbbf Mon Sep 17 00:00:00 2001
From: Tim Stahlhut <stahta01@gmail.com>
Date: Thu, 27 Jun 2019 09:39:29 -0400
Subject: don't escape arguments that don't need it in pex win32.c
Created by someone other than me.
---
libiberty/pex-win32.c | 47 ++++++++++++++++++++++++++++++++++---------
1 file changed, 38 insertions(+), 9 deletions(-)
diff --git a/libiberty/pex-win32.c b/libiberty/pex-win32.c
index 107ac6fdcc1..927eadefc2a 100644
--- a/libiberty/pex-win32.c
+++ b/libiberty/pex-win32.c
@@ -340,17 +340,26 @@ 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, \n \t \v 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] == '\n' ||
+ argv[i][j] == '\t' || argv[i][j] == '"' )
+ {
+ needs_quotes = 1;
+ }
+
if (argv[i][j] == '"')
{
/* Escape preceeding backslashes. */
@@ -362,16 +371,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 */
+ cmdline_len += 1 + (needs_quotes<<1); /* for leading and trailing quotes and space */
}
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] == '\n' ||
+ 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 +408,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';
--