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

Fix null comparison in Fido.ps1 #84

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Zigler21
Copy link

This pull request fixes the null comparison issue in the Fido.ps1 file. The comparison was incorrectly written as $winVersionId -eq $null, and it has been corrected to $null -eq $winVersionId. This ensures that the comparison is done correctly and resolves the issue.

@pbatard
Copy link
Owner

pbatard commented Dec 22, 2023

Please describe the actual issue, with an error that can be replicated, because as far as I know, $A -eq $B vs $B -eq $A is a matter of personal preferences. All the $winXXX have been initialized to $null at the beginning of the function, so I currently fail to see what issue this PR is trying to address...

@Hrxn
Copy link

Hrxn commented Dec 22, 2023

It works in both ways, but the PR is kind of right, because there's a potential pitfall in PowerShell.

https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_comparison_operators?view=powershell-7.4#equality-operators

  • When the left-hand side is scalar, -eq returns True if the right-hand side is equivalent, otherwise, -eq returns False. -ne does the opposite; it returns False when both sides are equivalent; otherwise, -ne returns True.
  • When the left-hand side is a collection, -eq returns those members that match the right-hand side, while -ne filters them out.

That explains the difference in functionality of LHS vs RHS.
The potential pitfall is if the LHS is unexpectedly a collection.

There is a rule in PSScriptAnalyzer about this.

Doc page of this rule, PossibleIncorrectComparisonWithNull.

I quote from this page:

To ensure that PowerShell performs comparisons correctly, the $null element should be on the left side of the operator.

There are multiple reasons why this occurs:

  • $null is a scalar value. When the value on the left side of an operator is a scalar, comparison operators return a Boolean value. When the value is a collection, the comparison operators return any matching values or an empty array if there are no matches in the collection.
  • PowerShell performs type casting left to right, resulting in incorrect comparisons when $null is cast to other scalar types.

The only way to reliably check if a value is $null is to place $null on the left side of the operator so that a scalar comparison is performed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants