4
4
4ainik2018-12-06 11:21:44
Command line
4ainik, 2018-12-06 11:21:44

Why do command line options break if they end with a slash?

I came across a very interesting case, in my opinion this is a bug in its purest form, but very rare and difficult to detect.
The main point is that the program/script is called with the passing of parameters containing spaces enclosed in double quotes. At the same time, if the parameter contains a trailing slash (in windows style!), then a failure occurs when parsing parameters at the level of ruby/php interpreters, or maybe binaries have the same problem?!

php script.php "param1 with space and leading slash\" "param2 with space too"

ruby script.rb "param1 with space and leading slash\" "param2 with space too"

In general, in this case, the script is called with only two parameters, but at the output (more precisely, at the script input), there are more arguments in the corresponding variables than necessary.
Checked on the binary, the same nonsense. And one would expect anything but this:

C:\cmd_line_test>
cmd_line_test.exe "param1 with space and leading slash\" "param2 with space too"
argc=5
argv[0]=|C:\cmd_line_test\cmd_line_test.exe|
argv[1]=|param1 with space and leading slash" param2|
argv[2]=|with|
argv[3]=|space|
argv[4]=|too|

Let's say a slash is used for escape, but that's nonsense?! If all paths in Windows are used with such slashes "\", then this must be a highly intelligent interpreter with an escape?!
At the same time, the funny thing is that in this case it turns out neither two nor one and a half, more precisely, just one and a half and it comes out, because if it comes to that, it is the line
param1 with space and leading slash\"
should be the first argument, if we take into account the escape rule
\"
?!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
F
Fat Lorrie, 2018-12-06
@Free_ze

trailing slash (windows-style!)

This is called backslash/backslash .
because for that matter, it is the line
param1 with space and leading slash\"
that should be the first argument, if we take into account the escape rule as well

You got it right about escape. But:
.
Must read minds?

K
Konstantin Tsvetkov, 2018-12-06
@tsklab

test.cmd:

@echo %1
@echo %2

test.cmd "param1 with space and leading slash\" "param2 with space too"
"param1 with space and leading slash\"
"param2 with space too"

program ... glitches
There are no errors with the program either:
program Project1;
{$APPTYPE CONSOLE}
uses System.SysUtils;
  var i: Integer;
begin
  for i := 0 to ParamCount do Writeln( ParamStr(i) ); // uses GetCommandLineW
end.

Project1.exe  "param1 with space and leading slash\" "param2 with space too"
Project1.exe
param1 with space and leading slash\
param2 with space too

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question