Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

Ticket #1053 (closed defect: fixed)

Opened 16 years ago

Last modified 16 years ago

tango.sys.Process.toString doesn't escape quotes in arguments

Reported by: Deewiant Assigned to: schveiguy
Priority: major Milestone: 0.99.6
Component: Core Functionality Version: 0.99.5 Jascha
Keywords: Cc:

Description

Consider the following simple program.

import tango.sys.Process;
import tango.io.Stdout;

void main() {
	auto p = new Process("echo", "Jack said \"foo\"");
	p.execute();
	Stderr.stream.copy(p.stdout);
	Stderr(p).newline;
}

Currently it outputs:

Jack said foo
echo "Jack said "foo""

The problem becomes clear in the output of toString. The quotes in the argument aren't escaped which results in incorrect output. A simple change to Process.toString would result in the expected:

Jack said "foo"
echo "Jack said \"foo\""

The change in question (svn diff):

===================================================================
--- Process.d   (revision 3343)
+++ Process.d   (working copy)
@@ -368,7 +368,7 @@
             if (contains(_args[i], ' ') || _args[i].length == 0)
             {
                 command ~= '"';
-                command ~= _args[i];
+                command ~= _args[i].substitute(`"`, `\"`);
                 command ~= '"';
             }
             else

Change History

04/16/08 10:49:44 changed by Deewiant

  • owner changed from sean to schveiguy.

04/16/08 14:10:07 changed by Deewiant

Of course my patch doesn't handle backslashes in _args[i] properly, those would also have to be substituted.

04/16/08 14:16:32 changed by schveiguy

  • status changed from new to assigned.
  • milestone changed from 0.99.7 to 0.99.6.

Backslashes aren't important. The issue is only on windows, where one cannot pass the array of arguments as an array, one must create one big string which has the arguments separated by spaces. The only significant things to escape are spaces, which must be surrounded by quotes, and quotes, which are discarded by the CreateProcess? function.

So I think your patch will do nicely. Thanks!

04/16/08 14:20:43 changed by schveiguy

Hm... on second thought, you are probably right. I'll re-check the behavior of CreateProcess? before checking the fix in.

04/16/08 19:51:39 changed by schveiguy

  • status changed from assigned to closed.
  • resolution set to fixed.

(In [3433]) Fixes #1053 by escaping all quotes and backslashes as necessary, thanks Deewiant!