Bug 573677 - Terminal splits command by whitespace, yet works anyway?
Summary: Terminal splits command by whitespace, yet works anyway?
Status: NEW
Alias: None
Product: CDT
Classification: Tools
Component: terminal (show other bugs)
Version: Next   Edit
Hardware: PC All
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: Project Inbox CLA
QA Contact: Jonah Graham CLA
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2021-05-20 20:47 EDT by Jonah Graham CLA
Modified: 2021-06-14 22:20 EDT (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Jonah Graham CLA 2021-05-20 20:47:06 EDT
In org.eclipse.tm.terminal.connector.process.ProcessConnector.connect(ITerminalControl) there is a StreamTokenizer used to convert the command line into an array, which is basically a split on whitespace.

This means that by the time the Spawner receives a command that started like this:

"C:\Program Files\Git\bin\sh.exe --login -i"

it looks like this:

[C:\Program, Files\Git\bin\sh.exe, --login, -i]

Amazingly the native code in CDT (or Windows itself) has a matching bug that permits this and puts it back together correctly.
Comment 1 Jonah Graham CLA 2021-05-20 22:47:28 EDT
This is a defect that does not appear most of the time because on Windows (where spaces are more typical) the split string in rejoined with spaces and passed to CreateProcess which takes a single string. So the dodgy split is never noticed. On Linux, since spaces are less typical, there is no join happening later because execve/execv is used that takes the array.

I would have to jump on my Linux machine to test what actually would happen (what sort of error?) if the launch happened on a Linux machine with a space in the executable name.
Comment 2 Jonah Graham CLA 2021-05-20 22:48:32 EDT
(In reply to Jonah Graham from comment #0)
> This means that by the time the Spawner receives a command that started like
> this:
> 
> "C:\Program Files\Git\bin\sh.exe --login -i"

Actually it does not start like ^^^ but instead starts as two different strings:
"C:\Program Files\Git\bin\sh.exe" and "--login -i" which are joined together, and then immediately split.
Comment 3 Jonah Graham CLA 2021-05-21 08:20:32 EDT
Arrgh! The concatenation in Windows is even wrong, but works anyway....

If you are using a long file name that contains a space, use quoted strings to indicate where the file name ends and the arguments begin (see the explanation for the lpApplicationName parameter) (from https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessw) -- but because Windows can't break old bugs it seems that doing an unquoted C:\Program Files... works anyway.


https://docs.microsoft.com/en-ca/archive/blogs/twistylittlepassagesallalike/everyone-quotes-command-line-arguments-the-wrong-way for the right way to do the concatenation - assumes that the array was correct in the first place.
Comment 4 Eclipse Genie CLA 2021-05-23 00:21:08 EDT
New Gerrit change created: https://git.eclipse.org/r/c/cdt/org.eclipse.cdt/+/180923
Comment 5 Jonah Graham CLA 2021-05-23 09:41:02 EDT
One side effect of this bug is that on Windows when OpenProcess is used we are limited to MAX_PATH for the the executable name because we don't use the first argument of OpenProcess.
Comment 6 Jonah Graham CLA 2021-05-23 14:56:39 EDT
I am not changing this now, it should be fixed soon, but I want to decouple it from the other work.

See https://git.eclipse.org/r/c/cdt/org.eclipse.cdt/+/180894/2/core/org.eclipse.cdt.core.native/src/org/eclipse/cdt/utils/pty/ConPTY.java#74 for a place that should be changed once the fix here is done.
Comment 8 Eclipse Genie CLA 2021-06-14 15:48:38 EDT
New Gerrit change created: https://git.eclipse.org/r/c/cdt/org.eclipse.cdt/+/181944
Comment 9 Eclipse Genie CLA 2021-06-14 15:48:39 EDT
New Gerrit change created: https://git.eclipse.org/r/c/cdt/org.eclipse.cdt/+/181945