BUG OF THE MONTH | Potential null dereference

V3080 Possible null dereference. Consider inspecting ‘ItemSelectionCondition’. System.Management.Automation displayDescriptionData_List.cs 352

internal bool SafeForExport()
  return DisplayEntry.SafeForExport() &&
         ItemSelectionCondition == null 
      || ItemSelectionCondition.SafeForExport();

There is a risk of getting a NullReferenceException when executing this code. The ItemSelectionCondition.SafeForExport() subexpression will be evaluated only if the first subexpression evaluates to false. Therefore, if DisplayEntry.SafeForExport() returns false and ItemSelectionCondition == null, the second subexpression, ItemSelectionCondition.SafeForExport(), will be evaluated, and that’s where the null dereference will occur (and raise the exception). Correct variant can be like this:

internal bool SafeForExport()
  return (DisplayEntry.SafeForExport() && 
          ItemSelectionCondition == null)
      || (ItemSelectionCondition != null &&

Please click here to see more bugs from this project.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.