Title: Some issues found by static analyzer · Issue #25289 · PowerShell/PowerShell · GitHub
Open Graph Title: Some issues found by static analyzer · Issue #25289 · PowerShell/PowerShell
X Title: Some issues found by static analyzer · Issue #25289 · PowerShell/PowerShell
Description: Prerequisites Write a descriptive title. Make sure you are able to repro it on the latest released version Search the existing issues. Refer to the FAQ. Refer to Differences between Windows PowerShell 5.1 and PowerShell. Steps to reprodu...
Open Graph Description: Prerequisites Write a descriptive title. Make sure you are able to repro it on the latest released version Search the existing issues. Refer to the FAQ. Refer to Differences between Windows PowerSh...
X Description: Prerequisites Write a descriptive title. Make sure you are able to repro it on the latest released version Search the existing issues. Refer to the FAQ. Refer to Differences between Windows PowerSh...
Opengraph URL: https://github.com/PowerShell/PowerShell/issues/25289
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Some issues found by static analyzer","articleBody":"### Prerequisites\n\n- [x] Write a descriptive title.\n- [x] Make sure you are able to repro it on the [latest released version](https://github.com/PowerShell/PowerShell/releases)\n- [x] Search the existing issues.\n- [x] Refer to the [FAQ](https://github.com/PowerShell/PowerShell/blob/master/docs/FAQ.md).\n- [x] Refer to [Differences between Windows PowerShell 5.1 and PowerShell](https://learn.microsoft.com/powershell/scripting/whats-new/differences-from-windows-powershell).\n\n### Steps to reproduce\n\nThis issues found by static analyzer, so we don't have steps to reproduce.\n\n### Expected behavior\n\n```console\nThe described errors are not in the project source code\n```\n\n### Actual behavior\n\n```console\nThe described errors are in the project source code\n```\n\n### Error details\n\n```console\n\n```\n\n### Environment data\n\n```powershell\nNo environment\n```\n\n### Visuals\n\nHello! We released [an article](https://pvs-studio.com/en/blog/posts/csharp/1243/) with checking this project with PVS-Studio static code analyzer and found some interesting bugs. Their description below.\n\n1. src\\Microsoft.Management.Infrastructure.CimCmdlets\\CimGetInstance.cs, line 168\n\n```cs\n....\ncase CimBaseCommand.CimInstanceSessionSet:\n\tCimInstance instance = GetCimInstanceParameter(cmdlet);\n\tnameSpace = ConstValue.GetNamespace(instance.CimSystemProperties.Namespace);\n\t foreach (CimSessionProxy proxy in proxys)\n\t {\n\t proxy.GetInstanceAsync(nameSpace, instance);\n\t }\n....\n```\n\nPVS-Studio Message:\n[V3080](https://pvs-studio.com/en/docs/warnings/v3080/) Possible null dereference. Consider inspecting 'instance'. CimGetInstance.cs 168\n\n`instance` is nullable because `GetCimInstance` method can return `null`:\n\n```cs\nprotected static CimInstance GetCimInstanceParameter(CimBaseCommand cmdlet)\n{\n\tif (cmdlet is GetCimInstanceCommand)\n\t{\n\t\treturn (cmdlet as GetCimInstanceCommand).CimInstance;\n\t}\n\t\n\telse if (cmdlet is RemoveCimInstanceCommand)\n\t{\n\t\treturn (cmdlet as RemoveCimInstanceCommand).CimInstance;\n\t}\n\t\n\telse if (cmdlet is SetCimInstanceCommand)\n\t{\n\t\treturn (cmdlet as SetCimInstanceCommand).CimInstance;\n\t}\n\treturn null;\n}\n```\n\n2. src\\Microsoft.Management.Infrastructure.CimCmdlets\\Utils.cs, line 305\n\n```cs\nprivate static bool logInitialized = false;\n....\nif (!logInitialized)\n{\n\tlock (logLock)\n\t{\n\t\tif (!logInitialized)\n\t\t{\n\t\t\tDebugHelper.GenerateLog = File.Exists(logFile);\n\t\t\tlogInitialized = true;\n\t\t}\n\t}\n}\n....\n```\n\nPVS-Studio Message: \n[V3054](https://pvs-studio.com/en/docs/warnings/v3054) Potentially unsafe double-checked locking. Use volatile variable(s) or synchronization primitives to avoid this. Utils.cs 305\n\nHere analyzer found unsafe double-checked locking because `logInitialized` field isn't declared as `volatile`.\n\n3. src\\Microsoft.PowerShell.Commands.Diagnostics\\GetEventCommand.cs, line 1066\n\n```cs\nqueriedLogsQueryMapSuppress.Add(logName.ToLowerInvariant(),\n string.Format(CultureInfo.InvariantCulture, \n suppressOpener, \n queryId++, \n logName)\n );\n```\n\nPVS-Studio Message:\n[V3025](https://pvs-studio.com/en/docs/warnings/v3025/) Incorrect format. A different number of format items is expected while calling 'Format' function. Arguments not used: queryId++, logName. GetEventCommand.cs 1067\n\nHere analyzer warns about string formatting because `suppressOpener` is `\"\u003cSuppress\u003e*\"` and doesn't have any placeholders. Perhaps there should be another variable here.\n\n4. src\\Microsoft.PowerShell.Commands.Utility\\commands\\utility\\New-Object.cs, line 3002\n\n```cs\n....\nswitch (Context.LanguageMode)\n{ \n\tcase PSLanguageMode.ConstrainedLanguage: \n\t\t.... \n\t\tbreak; \n\tcase PSLanguageMode.NoLanguage: \n\tcase PSLanguageMode.RestrictedLanguage: \n\t.... \n\tbreak;\n}\n....\n```\n\nPVS-Studio Message:\n[V3002](https://pvs-studio.com/en/docs/warnings/v3002/) The switch statement does not cover all values of the 'PSLanguageMode' enum: FullLanguage. New-Object.cs 190 undefined\n\nThis switch doesn't cover all vallues of `PSLanguageMode` enumeration and doesn't have `deafult` branch.\n\n5. src\\Microsoft.PowerShell.Commands.Utility\\commands\\utility\\ShowCommand\\ShowCommandCommandInfo.cs, line 81\n```cs\n...\nthis.Module = other.Members[\"Module\"].Value as ShowCommandModuleInfo;\n...\nif (other.Members[\"Module\"]?.Value is PSObject)\n```\n\nPVS-Studio Message: \n[V3095](https://pvs-studio.com/en/docs/warnings/v3095/) The 'other.Members[\"Module\"]' object was used before it was verified against null. Check lines: 81, 91. ShowCommandCommandInfo.cs 81\n\n`other.Members` is being checked against `null` below in the code, but in this location it isn't.\n\n6. src\\Microsoft.PowerShell.ConsoleHost\\host\\msh\\ConsoleHost.cs, line 781\n\n```cs\ninternal LocalRunspace LocalRunspace\n{\n\tget\n\t{\n\t\tif (_isRunspacePushed)\n\t\t{\n\t\t\treturn RunspaceRef.OldRunspace as LocalRunspace;\n\t\t}\n\n\t\tif (RunspaceRef == null)\n\t\t{\n\t\t\treturn null;\n\t\t}\n\t\t\n\t\treturn RunspaceRef.Runspace as LocalRunspace;\n\t}\n}\n```\n\nPVS-Studio Message:\n[V3095](https://pvs-studio.com/en/docs/warnings/v3095/) The 'RunspaceRef' object was used before it was verified against null. Check lines: 781, 784. ConsoleHost.cs 781\n\nI think this message doesn't need any explanation.\n\n7. src\\System.Management.Automation\\FormatAndOutput\\common\\DisplayDatabase\\typeDataQuery.cs, line 415\n\n```cs\n....\nif (vd == null || mainControlType != vd.mainControl.GetType())\n{\n\tActiveTracer.WriteLine(\n\t\t\"NOT MATCH {0} NAME: {1}\",\n\t\tControlBase.GetControlShapeName(vd.mainControl), \n\t\t(vd != null ? vd.name : string.Empty)\n\t);\n continue;\n}\n....\n```\n\nPVS-Studio Message:\n[V3095](https://pvs-studio.com/en/docs/warnings/v3095/) The 'vd' object was used before it was verified against null. Check lines: 415, 415. typeDataQuery.cs 415\n\nIf `vd` will be `null` then `ControlBase.GetControlShapeName(vd.mainControl)` will lead to `NullReferenceException`.\n\n8. src\\System.Management.Automation\\cimSupport\\cmdletization\\xml\\CoreCLR\\cmdlets-over-objects.xmlSerializer.autogen.cs, line 1471\n\n```cs\nif (Reader.NodeType == System.Xml.XmlNodeType.Element)\n{\n\tUnknownNode((object)o, string.Empty);\n}\nelse\n{\n\tUnknownNode((object)o, string.Empty);\n}\n```\n\nPVS-Studio Message:\n[V3004](https://pvs-studio.com/en/docs/warnings/v3004/) The 'then' statement is equivalent to the 'else' statement. cmdlets-over-objects.xmlSerializer.autogen.cs 1471\n\nYes, just about refactoring.\n\n9. src\\System.Management.Automation\\utils\\StringUtil.cs, line 246\n\n```cs\nint capacity = length + prependStr?.Length ?? 0 + appendStr?.Length ?? 0;\n```\n\nPVS-Studio Message:\n[V3123](https://pvs-studio.com/en/docs/warnings/v3123/) Perhaps the '??' operator works in a different way than it was expected. Its priority is lower than priority of other operators in its left part. StringUtil.cs 246\n\nThis expression may have unexpected result because `??` operator have lower priority than `+`.\n\n10. src\\System.Management.Automation\\engine\\hostifaces\\Command.cs, line 779\n\n```cs\n[Flags]\npublic enum PipelineResultTypes\n{\n\tNone = 1,\n Output = 2,\n Error = 4,\n Warning = 8,\n Verbose = 16,\n Debug = 32,\n Information = 64,\n All = 128,\n Null = 0\n}\n```\n\nPVS-Studio Message:\n[V3121](https://pvs-studio.com/en/docs/warnings/v3121/) An enumeration 'PipelineResultTypes' was declared with 'Flags' attribute, but does not set any initializers to override default values. Command.cs 779\n\nThis `enum` is declared with `Flags` attribute but doesn't override default values so it may take an unexpected effect when combining elements. For example, result of this expression would be `PipelineResultTypes.Warning`:\n\n```cs\n_mergeUnclaimedPreviousCommandResults = PipelineResultTypes.Error | PipelineResultTypes.Output;\n```","author":{"url":"https://github.com/feeelin","@type":"Person","name":"feeelin"},"datePublished":"2025-04-08T07:32:36.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":1},"url":"https://github.com/25289/PowerShell/issues/25289"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:b26f0b5d-e6bc-c9c0-3c33-ffe4bbb6041e |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B354:3C8683:1C4F006:26B0946:6A4F249F |
| html-safe-nonce | 53d6db37253f1070718ec8b1c9ac1f7d4fe8c33ebb7bde8ff0e0b7011d6c65a8 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCMzU0OjNDODY4MzoxQzRGMDA2OjI2QjA5NDY6NkE0RjI0OUYiLCJ2aXNpdG9yX2lkIjoiMzkzNjE5NzU1NTU4MzM5NDk3NSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 139e2d2e0cc3c660eb4094792ee710bf524ef31f0d33ec60c777cb2016d10c27 |
| hovercard-subject-tag | issue:2978884717 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/PowerShell/PowerShell/25289/issue_layout |
| twitter:image | https://opengraph.githubassets.com/2621a3f5bf67306bb332f8d617fbc4f472d7a515e4d4a03f4391e86fab8b4641/PowerShell/PowerShell/issues/25289 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/2621a3f5bf67306bb332f8d617fbc4f472d7a515e4d4a03f4391e86fab8b4641/PowerShell/PowerShell/issues/25289 |
| og:image:alt | Prerequisites Write a descriptive title. Make sure you are able to repro it on the latest released version Search the existing issues. Refer to the FAQ. Refer to Differences between Windows PowerSh... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | feeelin |
| hostname | github.com |
| expected-hostname | github.com |
| None | b92d11c0aa4a77d54ef4af1078b6a15fb5a70a215b30c4ecf28889d5a8e656d9 |
| turbo-cache-control | no-preview |
| go-import | github.com/PowerShell/PowerShell git https://github.com/PowerShell/PowerShell.git |
| octolytics-dimension-user_id | 11524380 |
| octolytics-dimension-user_login | PowerShell |
| octolytics-dimension-repository_id | 49609581 |
| octolytics-dimension-repository_nwo | PowerShell/PowerShell |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 49609581 |
| octolytics-dimension-repository_network_root_nwo | PowerShell/PowerShell |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 2b8f23afb982271f1b22258a94aede67a6b77760 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width