Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions src/System.Management.Automation/engine/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -809,11 +809,11 @@ internal static bool IsValidPSEditionValue(string editionValue)

private static readonly Dictionary<string, string> WindowsPowershellGroupPolicyKeys = new Dictionary<string, string>
{
{nameof(ScriptExecution), @"Software\Policies\Microsoft\Windows\PowerShell"},
{nameof(ScriptBlockLogging), @"Software\Policies\Microsoft\Windows\PowerShell\ScriptBlockLogging"},
{nameof(ModuleLogging), @"Software\Policies\Microsoft\Windows\PowerShell\ModuleLogging"},
{nameof(Transcription), @"Software\Policies\Microsoft\Windows\PowerShell\Transcription"},
{nameof(UpdatableHelp), @"Software\Policies\Microsoft\Windows\PowerShell\UpdatableHelp"},
{ nameof(ScriptExecution), @"Software\Policies\Microsoft\Windows\PowerShell" },
{ nameof(ScriptBlockLogging), @"Software\Policies\Microsoft\Windows\PowerShell\ScriptBlockLogging" },
{ nameof(ModuleLogging), @"Software\Policies\Microsoft\Windows\PowerShell\ModuleLogging" },
{ nameof(Transcription), @"Software\Policies\Microsoft\Windows\PowerShell\Transcription" },
{ nameof(UpdatableHelp), @"Software\Policies\Microsoft\Windows\PowerShell\UpdatableHelp" },
};

private const string PolicySettingFallbackKey = "UseWindowsPowerShellPolicySetting";
Expand Down Expand Up @@ -858,7 +858,10 @@ private static bool TrySetPolicySettingsFromRegistryKey(object instance, Type in
{
using (RegistryKey subKey = gpoKey.OpenSubKey(settingName))
{
if (subKey != null) { rawRegistryValue = subKey.GetValueNames(); }
if (subKey != null)
{
rawRegistryValue = subKey.GetValueNames();
}
}
}

Expand All @@ -874,8 +877,14 @@ private static bool TrySetPolicySettingsFromRegistryKey(object instance, Type in
case var _ when propertyType == typeof(bool?):
if (rawRegistryValue is int rawIntValue)
{
if (rawIntValue == 1) { propertyValue = true; }
else if (rawIntValue == 0) { propertyValue = false; }
if (rawIntValue == 1)
{
propertyValue = true;
}
else if (rawIntValue == 0)
{
propertyValue = false;
}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a fairly straightforward bit of logic; perhaps it might be a bit tidier here using ternary syntax?

    propertyValue = rawIntValue == 1 ? true : false;

Unless there's actually a third possible outcome here, but that seems like something that isn't handled here anyway. 🙂

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vexx32 Thanks! You are right but no code changes is in the PR, only formatting. I don't want second review round in the simple style PR.

}

break;
Expand Down
31 changes: 16 additions & 15 deletions src/System.Management.Automation/engine/hostifaces/PSTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public PSTask(
ScriptBlock scriptBlock,
Dictionary<string, object> usingValuesMap,
object dollarUnderbar,
PSTaskDataStreamWriter dataStreamWriter)
PSTaskDataStreamWriter dataStreamWriter)
: base(
scriptBlock,
usingValuesMap,
Expand Down Expand Up @@ -127,7 +127,7 @@ private void HandleInformationData()
new PSStreamObject(PSStreamObjectType.Information, item));
}
}

#endregion

#region Event handlers
Expand Down Expand Up @@ -361,8 +361,8 @@ public PSInvocationState State

#region Constructor

private PSTaskBase()
{
private PSTaskBase()
{
_id = Interlocked.Increment(ref s_taskId);
}

Expand Down Expand Up @@ -422,7 +422,7 @@ public void Start()

// Create and open Runspace for this task to run in
var iss = InitialSessionState.CreateDefault2();
iss.LanguageMode = (SystemPolicy.GetSystemLockdownPolicy() == SystemEnforcementMode.Enforce)
iss.LanguageMode = (SystemPolicy.GetSystemLockdownPolicy() == SystemEnforcementMode.Enforce)
? PSLanguageMode.ConstrainedLanguage : PSLanguageMode.FullLanguage;
_runspace = RunspaceFactory.CreateRunspace(iss);
_runspace.Name = string.Format(CultureInfo.InvariantCulture, "{0}:{1}", RunspaceName, s_taskId);
Expand Down Expand Up @@ -475,7 +475,7 @@ internal sealed class PSTaskDataStreamWriter : IDisposable
private readonly PSCmdlet _cmdlet;
private readonly PSDataCollection<PSStreamObject> _dataStream;
private readonly int _cmdletThreadId;

#endregion

#region Properties
Expand Down Expand Up @@ -711,6 +711,7 @@ public bool Add(PSTaskBase task)

task.Start();
}

return true;

case Stop:
Expand Down Expand Up @@ -739,7 +740,7 @@ public void StopAll()
// Accept no more input
Close();
_stopAll.Set();

// Stop all running tasks
lock (_syncObject)
{
Expand All @@ -764,7 +765,7 @@ public void Close()
#region Private Methods

private void HandleTaskStateChangedDelegate(object sender, PSInvocationStateChangedEventArgs args) => HandleTaskStateChanged(sender, args);

private void HandleTaskStateChanged(object sender, PSInvocationStateChangedEventArgs args)
{
var task = sender as PSTaskBase;
Expand Down Expand Up @@ -805,10 +806,10 @@ private void CheckForComplete()
try
{
PoolComplete.SafeInvoke(
this,
this,
new EventArgs());
}
catch
catch
{
Dbg.Assert(false, "Exceptions should not be thrown on event thread");
}
Expand Down Expand Up @@ -872,8 +873,8 @@ public override string Location
/// </summary>
public override bool HasMoreData
{
get
{
get
{
foreach (var childJob in ChildJobs)
{
if (childJob.HasMoreData)
Expand Down Expand Up @@ -901,7 +902,7 @@ public override void StopJob()
{
_stopSignaled = true;
SetJobState(JobState.Stopping);

_taskPool.StopAll();
SetJobState(JobState.Stopped);
}
Expand Down Expand Up @@ -953,7 +954,7 @@ public void Start()
// This thread will end once all jobs reach a finished state by either running
// to completion, terminating with error, or stopped.
System.Threading.ThreadPool.QueueUserWorkItem(
(_) =>
(_) =>
{
foreach (var childJob in ChildJobs)
{
Expand Down Expand Up @@ -991,7 +992,7 @@ private void HandleTaskPoolComplete(object sender, EventArgs args)

SetJobState(finalState);
}
catch (ObjectDisposedException)
catch (ObjectDisposedException)
{ }
}

Expand Down
38 changes: 23 additions & 15 deletions src/System.Management.Automation/engine/parser/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6621,14 +6621,16 @@ private ExpressionAst BinaryExpressionRule(bool endNumberOnTernaryOpChars = fals
if (expr == null)
{
// ErrorRecovery: create an error expression to fill out the ast and keep parsing.

IScriptExtent extent = After(token);

// Use token.Text, not token.Kind.Text() b/c the kind might not match the actual operator used
// when a case insensitive operator is used.
ReportIncompleteInput(extent,
ReportIncompleteInput(
extent,
nameof(ParserStrings.ExpectedValueExpression),
ParserStrings.ExpectedValueExpression,
token.Text);

expr = new ErrorExpressionAst(extent);
}

Expand All @@ -6652,7 +6654,9 @@ private ExpressionAst BinaryExpressionRule(bool endNumberOnTernaryOpChars = fals
Token op = operatorStack.Pop();
operandStack.Push(new BinaryExpressionAst(ExtentOf(lhs, rhs), lhs, op.Kind, rhs, op.Extent));
if (operatorStack.Count == 0)
{
break;
}
precedence = operatorStack.Peek().Kind.GetBinaryPrecedence();
}

Expand Down Expand Up @@ -6710,7 +6714,6 @@ private ExpressionAst ArrayLiteralRule(bool endNumberOnTernaryOpChars = false)
// G array-literal-expression:
// G unary-expression
// G unary-expression ',' new-lines:opt array-literal-expression

ExpressionAst lastExpr = UnaryExpressionRule(endNumberOnTernaryOpChars);
if (lastExpr == null)
{
Expand All @@ -6737,7 +6740,6 @@ private ExpressionAst ArrayLiteralRule(bool endNumberOnTernaryOpChars = false)
if (lastExpr == null)
{
// ErrorRecovery: create an error expression for the ast and break.

ReportIncompleteInput(After(commaToken),
nameof(ParserStrings.MissingExpressionAfterToken),
ParserStrings.MissingExpressionAfterToken,
Expand Down Expand Up @@ -6851,13 +6853,15 @@ private ExpressionAst UnaryExpressionRule(bool endNumberOnTernaryOpChars = false
{
// ErrorRecovery: don't bother constructing a unary expression, but we know we must have
// some sort of expression, so return an error expression.

//
// Use token.Text, not token.Kind.Text() b/c the kind might not match the actual operator used
// when a case insensitive operator is used.
ReportIncompleteInput(After(token),
ReportIncompleteInput(
After(token),
nameof(ParserStrings.MissingExpressionAfterOperator),
ParserStrings.MissingExpressionAfterOperator,
token.Text);

return new ErrorExpressionAst(token.Extent);
}
}
Expand All @@ -6881,28 +6885,31 @@ private ExpressionAst UnaryExpressionRule(bool endNumberOnTernaryOpChars = false
{
// ErrorRecovery: We have a list of attributes, and we know it's not before a param statement,
// so we know we must have some sort of expression. Return an error expression then.

ReportIncompleteInput(lastAttribute.Extent,
ReportIncompleteInput(
lastAttribute.Extent,
nameof(ParserStrings.UnexpectedAttribute),
ParserStrings.UnexpectedAttribute,
lastAttribute.TypeName.FullName);

return new ErrorExpressionAst(ExtentOf(token, lastAttribute));
}

expr = new AttributedExpressionAst(ExtentOf(lastAttribute, child), lastAttribute, child);
}
else
{
Diagnostics.Assert(_ungotToken == null || ErrorList.Count > 0,
"Unexpected lookahead from AttributeListRule.");
Diagnostics.Assert(
_ungotToken == null || ErrorList.Count > 0,
"Unexpected lookahead from AttributeListRule.");

// If we've looked ahead, don't go looking for a member access token, we've already issued an error,
// just assume we're not trying to access a member.
var memberAccessToken = _ungotToken != null ? null : NextMemberAccessToken(false);
if (memberAccessToken != null)
{
expr = CheckPostPrimaryExpressionOperators(memberAccessToken,
new TypeExpressionAst(lastAttribute.Extent,
lastAttribute.TypeName));
expr = CheckPostPrimaryExpressionOperators(
memberAccessToken,
new TypeExpressionAst(lastAttribute.Extent, lastAttribute.TypeName));
}
else
{
Expand All @@ -6913,8 +6920,9 @@ private ExpressionAst UnaryExpressionRule(bool endNumberOnTernaryOpChars = false
child = UnaryExpressionRule(endNumberOnTernaryOpChars: true);
if (child != null)
{
expr = new ConvertExpressionAst(ExtentOf(lastAttribute, child),
(TypeConstraintAst)lastAttribute, child);
expr = new ConvertExpressionAst(
ExtentOf(lastAttribute, child),
(TypeConstraintAst)lastAttribute, child);
}
}
}
Expand Down
13 changes: 8 additions & 5 deletions src/System.Management.Automation/engine/parser/ast.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7104,7 +7104,7 @@ internal virtual bool ShouldPreserveOutputInCaseOfException()
public class TernaryExpressionAst : ExpressionAst
{
/// <summary>
/// Construct a binary expression.
/// Initializes a new instance of the a ternary expression.
/// </summary>
/// <param name="extent">The extent of the expression.</param>
/// <param name="condition">The condition operand.</param>
Expand All @@ -7123,23 +7123,26 @@ public TernaryExpressionAst(IScriptExtent extent, ExpressionAst condition, Expre
}

/// <summary>
/// The ast for the condition of the ternary expression. The property is never null.
/// Gets the ast for the condition of the ternary expression. The property is never null.
/// </summary>
public ExpressionAst Condition { get; }

/// <summary>
/// The ast for the if-operand of the ternary expression. The property is never null.
/// Gets the ast for the if-operand of the ternary expression. The property is never null.
/// </summary>
public ExpressionAst IfTrue { get; }

/// <summary>
/// The ast for the else-operand of the ternary expression. The property is never null.
/// Gets the ast for the else-operand of the ternary expression. The property is never null.
/// </summary>
public ExpressionAst IfFalse { get; }

/// <summary>
/// Copy the TernaryExpressionAst instance.
/// </summary>
/// <return>
/// Retirns a copy of the ast.
/// </return>
public override Ast Copy()
{
ExpressionAst newCondition = CopyElement(this.Condition);
Expand Down Expand Up @@ -7199,7 +7202,7 @@ internal override AstVisitAction InternalVisit(AstVisitor visitor)
public class BinaryExpressionAst : ExpressionAst
{
/// <summary>
/// Construct a binary expression.
/// Initializes a new instance of the binary expression.
/// </summary>
/// <param name="extent">The extent of the expression.</param>
/// <param name="left">The left hand operand.</param>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,9 @@ protected void Start(string initialCommand, PSRemotingCryptoHelperServer cryptoH
catch (Exception e)
{
PSEtwLog.LogOperationalError(
PSEventId.TransportError, PSOpcode.Open, PSTask.None,
PSEventId.TransportError,
PSOpcode.Open,
PSTask.None,
PSKeyword.UseAlwaysOperational,
Guid.Empty.ToString(),
Guid.Empty.ToString(),
Expand All @@ -395,7 +397,9 @@ protected void Start(string initialCommand, PSRemotingCryptoHelperServer cryptoH
e.StackTrace);

PSEtwLog.LogAnalyticError(
PSEventId.TransportError_Analytic, PSOpcode.Open, PSTask.None,
PSEventId.TransportError_Analytic,
PSOpcode.Open,
PSTask.None,
PSKeyword.Transport | PSKeyword.UseAlwaysAnalytic,
Guid.Empty.ToString(),
Guid.Empty.ToString(),
Expand Down Expand Up @@ -483,7 +487,7 @@ private OutOfProcessMediator() : base(true)
#region Static Methods

/// <summary>
/// Starts the out-of-process powershell server instance.
/// Starts the out-of-process powershell server instance.
/// </summary>
/// <param name="initialCommand">Specifies the initialization script.</param>
/// <param name="workingDirectory">Specifies the initial working directory. The working directory is set before the initial command.</param>
Expand Down
Loading