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

Calling the CLI from the outside with an empty command string (-c '') crashes PowerShell #10684

Open
mklement0 opened this issue Oct 2, 2019 · 4 comments

Comments

@mklement0
Copy link
Contributor

commented Oct 2, 2019

From within PowerShell, running pwsh -c '' (pwsh -Command '') provides a helpful error message (followed by the CLI syntax):

Cannot process the command because of a missing parameter. A command must follow -Command.

By contrast, when pwsh is called from the outside, it crashes.

Steps to reproduce

Run from either cmd.exe or bash:

pwsh -c ""

Expected behavior

The following should print to stderr and a nonzero exit code should be reported, analogous to what happens when calling from PowerShell.

Cannot process the command because of a missing parameter. A command must follow -Command.

(CLI syntax diagram)

Actual behavior

The pwsh process crashes as follows:

Unhandled exception. System.ArgumentNullException: Value cannot be null. (Parameter 'value')
   at System.String.IndexOf(String value, Int32 startIndex, Int32 count, StringComparison comparisonType)
   at System.String.IndexOf(String value, StringComparison comparisonType)
   at Microsoft.PowerShell.CommandLineParameterParser.MatchSwitch(String switchKey, String match, String smallestUnambiguousMatch)
   at Microsoft.PowerShell.CommandLineParameterParser.EarlyParse(String[] args)
   at Microsoft.PowerShell.UnmanagedPSEntry.Start(String consoleFilePath, String[] args, Int32 argc)
   at Microsoft.PowerShell.ManagedPSEntry.Main(String[] args)

Environment data

PowerShell Core 7.0.0-preview.4
@iSazonov

This comment has been minimized.

Copy link
Collaborator

commented Oct 3, 2019

We send null to MatchSwitch()

bool noexitSeen = false;
for (int i = 0; i < args.Length; ++i)
{
(string SwitchKey, bool ShouldBreak) switchKeyResults = GetSwitchKey(args, ref i, parser: null, ref noexitSeen);
if (switchKeyResults.ShouldBreak)
{
break;
}
string switchKey = switchKeyResults.SwitchKey;
if (MatchSwitch(switchKey, match: "settingsfile", smallestUnambiguousMatch: "settings"))
{
// parse setting file arg and don't write error as there is no host yet.
if (!TryParseSettingFileHelper(args, ++i, parser: null))
{
break;
}
}

We need to issue common user-friendly message.

@chuanjiao10

This comment has been minimized.

Copy link

commented Oct 3, 2019

@iSazonov I submitted the issue first, reported detailed system information(only linux), but was closed as Duplicate.

After submitting the issue, only write "PowerShell Core 7.0.0-preview.4",Actually only linux.
issue
it was used as an engine bug.10410 gt 10684??? This is not fair to me.

I don't know what the hidden rules are for you. Below, please start your sophistry.

I guess you will say that 【pwsh -c ”$pid”】 i was reported, and 【pwsh -c ”” him reported 】 is essentially different.

@iSazonov

This comment has been minimized.

Copy link
Collaborator

commented Oct 3, 2019

@chuanjiao10 I think it is one issue.

@mklement0

This comment has been minimized.

Copy link
Contributor Author

commented Oct 3, 2019

@chuanjiao10:

Your original issue had a misconception about how POSIX-like shells such as Bash evaluate pwsh -c "$pid".

#10410 (comment) explains the misconception in detail and offers solutions.

In short: When you call from Bash, pwsh -c "$pid" is effectively the same as pwsh -c "", because Bash expands $pid and - given that the variable doesn't exist - expands it to the empty string.

It is only pwsh -c "" that is the problem - that is, passing what (ultimately evaluates to) an empty string to -c (-Command) from outside of PowerShell - and that's why this issue was created: to focus on the real problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.