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

Quoting in Unix.create_process #2939

Closed
vicuna opened this issue Jul 15, 2004 · 3 comments
Closed

Quoting in Unix.create_process #2939

vicuna opened this issue Jul 15, 2004 · 3 comments
Labels

Comments

@vicuna
Copy link

vicuna commented Jul 15, 2004

Original bug ID: 2939
Reporter: administrator
Status: closed
Resolution: not a bug
Priority: normal
Severity: minor
Category: ~DO NOT USE (was: OCaml general)

Bug description

Full_Name: Jason Hickey
Version: 3.08
OS: Win32
Submission from: adsl-67-120-175-90.dsl.lsan03.pacbell.net (67.120.175.90)

In 3.06, win32unix/unix.ml, the Unix.create_process function did not quote the
command arguments.

let create_process prog args fd1 fd2 fd3 =
win_create_process prog (String.concat " " (Array.to_list args)) None
fd1 fd2 fd3

In 3.08, it does:

let make_cmdline args =
let maybe_quote f =
if String.contains f ' ' || String.contains f '"'
then Filename.quote f
else f in
String.concat " " (List.map maybe_quote (Array.to_list args))

let create_process prog args fd1 fd2 fd3 =
win_create_process prog (make_cmdline args) None fd1 fd2 fd3

It seems like a reasonable idea, but it doesn't quite work. Consider the
following call:

let pid = Unix.create_process "cmd.exe"
[|"cmd.exe";
"/C";
"ocamlc -pp "camlp4 arg" foo.ml
|] fd0 fd1 fd2

The 3.08 code will produce this command line:

cmd.exe /C "ocamlc -pp "camlp4 arg" foo.ml"

This quoting inversion is a problem...

Jason

@vicuna
Copy link
Author

vicuna commented Aug 6, 2004

Comment author: administrator

In 3.06, win32unix/unix.ml, the Unix.create_process function did not
quote the command arguments. [...] In 3.08, it does: [...]
It seems like a reasonable idea, but it doesn't quite work. Consider the
following call:
let pid = Unix.create_process "cmd.exe"
[|"cmd.exe";
"/C";
"ocamlc -pp "camlp4 arg" foo.ml
|] fd0 fd1 fd2
The 3.08 code will produce this command line:
cmd.exe /C "ocamlc -pp "camlp4 arg" foo.ml"
This quoting inversion is a problem...

I beg to differ. The 3.08 code will produce that (correctly quoted)
command line:

cmd.exe /C "ocamlc -pp "camlp4 arg" foo.ml

(you can check this by replacing cmd.exe with the result of compiling
the C program given below).

The problem is that cmd.exe is broken and doesn't strip the " properly
(unlike programs that use Microsoft's C standard library). It
therefore executes

ocamlc -pp "camlp4 arg" foo.ml

which ocamlc (correctly) parses as the following arguments:

ocamlc
-pp
"camlp4
arg"
foo.ml

instead of the expected

ocamlc -pp "camlp4 arg" foo.ml

which ocamlc would interpret as expected as

ocamlc
-pp
camlp4 arg
foo.ml

My only recommendation is not to use cmd.exe if at all possible.

Best wishes,

  • Xavier Leroy

#include <stdio.h>
#include <windows.h>

int main(int argc, char ** argv)
{
int i;
printf("Raw command line is:\n%s\n", GetCommandLine());
printf("Parsed command line is:\n");
for (i = 0; i < argc; i++) printf("\targv[%d] = %s\n", i, argv[i]);
return 0;
}

@vicuna
Copy link
Author

vicuna commented Aug 6, 2004

Comment author: administrator

The issue is in Windows' cmd.exe command-line parsing. ocaml does the right
thing here.

@vicuna vicuna closed this as completed Aug 6, 2004
@vicuna
Copy link
Author

vicuna commented Sep 7, 2004

Comment author: administrator

You are absolutely right. I missed this mail (hence my late reply), but
I'm sorry I caused the trouble!

Jason

Xavier Leroy wrote:

I beg to differ. The 3.08 code will produce that (correctly quoted)
command line:

cmd.exe /C "ocamlc -pp "camlp4 arg" foo.ml"
...
The problem is that cmd.exe is broken and doesn't strip the " properly
(unlike programs that use Microsoft's C standard library).
--
Jason Hickey http://www.cs.caltech.edu/~jyh
Caltech Computer Science Tel: 626-395-6568 FAX: 626-792-4257

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant