Skip to content

Commit

Permalink
Merge pull request #2504 from CyberShadow/pull-20140909-143654
Browse files Browse the repository at this point in the history
fix Issue 13447 - Do not escape process parameters unless necessary
  • Loading branch information
AndrejMitrovic committed Sep 9, 2014
2 parents d479da3 + 09a0b87 commit f35c219
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions std/process.d
Expand Up @@ -2324,7 +2324,7 @@ unittest
},
{
args : ["foo bar", "hello"],
windows : `"foo bar" ^"hello^"`,
windows : `"foo bar" hello`,
posix : `'foo bar' 'hello'`
},
{
Expand All @@ -2334,7 +2334,7 @@ unittest
},
{
args : ["foo bar", "hello", "world"],
windows : `"foo bar" ^"hello^" ^"world^"`,
windows : `"foo bar" hello world`,
posix : `'foo bar' 'hello' 'world'`
},
{
Expand Down Expand Up @@ -2454,10 +2454,12 @@ private char[] escapeWindowsArgumentImpl(alias allocator)(in char[] arg)
// * http://msdn.microsoft.com/en-us/library/windows/desktop/bb776391(v=vs.85).aspx
// * http://blogs.msdn.com/b/oldnewthing/archive/2010/09/17/10063629.aspx

// Calculate the total string size.
// Check if the string needs to be escaped,
// and calculate the total string size.

// Trailing backslashes must be escaped
bool escaping = true;
bool needEscape = false;
// Result size = input size + 2 for surrounding quotes + 1 for the
// backslash for each escaped character.
size_t size = 1 + arg.length + 1;
Expand All @@ -2466,6 +2468,7 @@ private char[] escapeWindowsArgumentImpl(alias allocator)(in char[] arg)
{
if (c == '"')
{
needEscape = true;
escaping = true;
size++;
}
Expand All @@ -2476,9 +2479,25 @@ private char[] escapeWindowsArgumentImpl(alias allocator)(in char[] arg)
size++;
}
else
{
if (c == ' ' || c == '\t')
needEscape = true;
escaping = false;
}
}

// Empty arguments need to be specified as ""
if (!arg.length)
needEscape = true;
else
// Arguments ending with digits need to be escaped,
// to disambiguate with 1>file redirection syntax
if (std.ascii.isDigit(arg[$-1]))
needEscape = true;

if (!needEscape)
return allocator(arg.length)[] = arg;

// Construct result string.

auto buf = allocator(size);
Expand Down Expand Up @@ -2533,7 +2552,7 @@ version(Windows) version(unittest)
`C:\Program Files\`,
];

enum CHARS = `_x\" *&^`; // _ is placeholder for nothing
enum CHARS = `_x\" *&^` ~ "\t"; // _ is placeholder for nothing
foreach (c1; CHARS)
foreach (c2; CHARS)
foreach (c3; CHARS)
Expand Down

0 comments on commit f35c219

Please sign in to comment.