Skip to content

Commit b3eb4a8

Browse files
authored
Fix Filename.quote_command handling of forward slashes in program path under Win32 (#13417)
1 parent 45e6f65 commit b3eb4a8

4 files changed

Lines changed: 21 additions & 3 deletions

File tree

Changes

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,10 @@ ___________
617617
from a DLL
618618
(Xavier Leroy, review by Miod Vallat)
619619

620+
- #13417: `Filename.quote_command`: fix handling of forward slashes in program
621+
path under Win32.
622+
(Nicolás Ojeda Bär, review by David Allsopp and Damien Doligez)
623+
620624
OCaml 5.2.0 (13 May 2024)
621625
-------------------------
622626

stdlib/filename.ml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,13 @@ Quoting commands for execution by cmd.exe is difficult.
219219
s;
220220
Buffer.contents b
221221
let quote_cmd_filename f =
222+
(* In cmd.exe, forward slashes in the program path (argument 0) are
223+
interpreted as introducing a flag. *)
224+
let f =
225+
if String.contains f '/' then
226+
String.map (function '/' -> '\\' | c -> c) f
227+
else f
228+
in
222229
if String.exists (function '\"' | '%' -> true | _ -> false) f then
223230
failwith ("Filename.quote_command: bad file name " ^ f)
224231
else if String.contains f ' ' then

testsuite/tests/lib-filename/quotecommand.ml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,17 @@ let cat_file f =
5353
let myecho =
5454
Filename.concat Filename.current_dir_name "my echo.exe"
5555

56-
let run ?stdin ?stdout ?stderr args =
56+
let run prog ?stdin ?stdout ?stderr args =
5757
flush Stdlib.stdout;
5858
let rc =
59-
Sys.command (Filename.quote_command myecho ?stdin ?stdout ?stderr args) in
59+
Sys.command (Filename.quote_command prog ?stdin ?stdout ?stderr args) in
6060
if rc > 0 then begin
61-
printf "!!! my echo failed\n";
61+
printf "!!! %s failed\n" prog;
6262
exit 2
6363
end
6464

6565
let _ =
66+
let run = run myecho in
6667
copy_file "myecho.exe" "my echo.exe";
6768
printf "-------- Spaces\n";
6869
run ["Lorem ipsum dolor"; "sit amet,"; "consectetur adipiscing elit,"];
@@ -100,3 +101,7 @@ let _ =
100101
"in voluptate"; "-out"; "velit esse cillum"; "-err"; "dolore"];
101102
cat_file "my file.tmp"; Sys.remove "my file.tmp";
102103
Sys.remove "my echo.exe"
104+
105+
let _ =
106+
printf "-------- Forward slashes in program position\n";
107+
run "./myecho.exe" ["alea iacta est"]

testsuite/tests/lib-filename/quotecommand.reference

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,5 @@ argv[4] = {|in reprehenderit|}
3636
argv[5] = {|in voluptate|}
3737
argv[7] = {|velit esse cillum|}
3838
argv[9] = {|dolore|}
39+
-------- Forward slashes in program position
40+
argv[1] = {|alea iacta est|}

0 commit comments

Comments
 (0)