Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Fix 6 PVS-Studio static analyzer findings
Address items 1, 2, 5, 6, 7, 9 from the PVS-Studio report in #25289:

- Item 9: Fix ?? operator precedence in StringUtil.cs — appendStr
  length was silently ignored in capacity calculation
- Item 2: Add volatile to logInitialized in CimCmdlets/Utils.cs
  for safe double-checked locking
- Item 6: Move RunspaceRef null check before first use in
  ConsoleHost.cs to prevent NullReferenceException
- Item 1: Add null guard for GetCimInstanceParameter return value
  in CimGetInstance.cs
- Item 5: Use null-conditional on Members["Module"] access in
  ShowCommandCommandInfo.cs
- Item 7: Split vd null check from type check in typeDataQuery.cs
  to prevent NullReferenceException in trace logging

Items 3, 4, 8, 10 are intentionally left — they require design
decisions, affect auto-generated code, or would be breaking changes.

Addresses #25289

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
  • Loading branch information
SufficientDaikon and claude committed Mar 24, 2026
commit 881d2bb93e08551c2e34a70ed43d9d3fa0a88f09
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,13 @@ protected void GetCimInstanceInternal(CimBaseCommand cmdlet)
case CimBaseCommand.CimInstanceSessionSet:
{
CimInstance instance = GetCimInstanceParameter(cmdlet);
nameSpace = ConstValue.GetNamespace(instance.CimSystemProperties.Namespace);
foreach (CimSessionProxy proxy in proxys)
if (instance != null)
{
proxy.GetInstanceAsync(nameSpace, instance);
nameSpace = ConstValue.GetNamespace(instance.CimSystemProperties.Namespace);
foreach (CimSessionProxy proxy in proxys)
{
proxy.GetInstanceAsync(nameSpace, instance);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ internal static class DebugHelper
/// <summary>
/// Whether the log been initialized.
/// </summary>
private static bool logInitialized = false;
private static volatile bool logInitialized = false;

internal static bool GenerateVerboseMessage { get; set; } = true;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public ShowCommandCommandInfo(PSObject other)
{
// Simple case - the objects are still live because they came from in-proc. Just cast them back
this.CommandType = (CommandTypes)(other.Members["CommandType"].Value);
this.Module = other.Members["Module"].Value as ShowCommandModuleInfo;
this.Module = other.Members["Module"]?.Value as ShowCommandModuleInfo;
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -821,14 +821,14 @@ internal LocalRunspace LocalRunspace
{
get
{
if (_isRunspacePushed)
if (RunspaceRef == null)
{
return RunspaceRef.OldRunspace as LocalRunspace;
return null;
}

if (RunspaceRef == null)
if (_isRunspacePushed)
{
return null;
return RunspaceRef.OldRunspace as LocalRunspace;
}

return RunspaceRef.Runspace as LocalRunspace;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -408,11 +408,17 @@ private static ViewDefinition GetView(PSPropertyExpressionFactory expressionFact
TypeMatch match = new TypeMatch(expressionFactory, db, typeNames);
foreach (ViewDefinition vd in db.viewDefinitionsSection.viewDefinitionList)
{
if (vd == null || mainControlType != vd.mainControl.GetType())
if (vd == null)
{
ActiveTracer.WriteLine("NOT MATCH null view definition");
continue;
}

if (mainControlType != vd.mainControl.GetType())
{
ActiveTracer.WriteLine(
"NOT MATCH {0} NAME: {1}",
ControlBase.GetControlShapeName(vd.mainControl), (vd != null ? vd.name : string.Empty));
ControlBase.GetControlShapeName(vd.mainControl), vd.name);
continue;
}

Expand Down
2 changes: 1 addition & 1 deletion src/System.Management.Automation/utils/StringUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ internal static string VtSubstring(this string str, int startOffset, int length,
}
else
{
int capacity = length + prependStr?.Length ?? 0 + appendStr?.Length ?? 0;
int capacity = length + (prependStr?.Length ?? 0) + (appendStr?.Length ?? 0);
return new StringBuilder(prependStr, capacity)
.Append(str, startOffset, length)
.Append(appendStr)
Expand Down