Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix Issue 13447 - Do not escape process parameters unless necessary #2504

Merged
1 commit merged into from
Sep 9, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions std/process.d
Original file line number Diff line number Diff line change
Expand Up @@ -2313,7 +2313,7 @@ unittest
},
{
args : ["foo bar", "hello"],
windows : `"foo bar" ^"hello^"`,
windows : `"foo bar" hello`,
posix : `'foo bar' 'hello'`
},
{
Expand All @@ -2323,7 +2323,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 @@ -2445,10 +2445,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 @@ -2457,6 +2459,7 @@ private char[] escapeWindowsArgumentImpl(alias allocator)(in char[] arg)
{
if (c == '"')
{
needEscape = true;
escaping = true;
size++;
}
Expand All @@ -2467,9 +2470,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 @@ -2524,7 +2543,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