C#


CSHARP.STRUCT.UPD : Unchecked Parameter Dereference (C#)

Summary

A parameter of a method or constructor is dereferenced but has not been tested for nullness.

If the null value gets dereferenced, C# runs into a NullReferenceException. For this reason, programmers must ensure that the content of expressions dereferenced in their programs is never null. Solving this problem is in general hard. This checker provides a coverage of the most frequent scenarios when null might end up being dereferenced. For a sound alternative to this checker, that covers all possible situations, see the Nullness checker. However, BasicNullness is much faster than Nullness and issues a more restricted set of false alarms, hence it is often the best solution for a rapid identification of the most frequent null-pointer errors in a program.

Strict and Non-Strict Checking

When CSHARP_ANALYSIS_STRICT_MODE=No, warnings of this class will not be issued if there are indications that the possibility of a NullReferenceException has been recognized and accounted for. For example, warnings will not be issued for code inside a try-catch block that explicitly catches NullReferenceException, or for a JUnit test that is annotated as expecting this exception.

When CSHARP_ANALYSIS_STRICT_MODE=Yes, warnings will be issued even in these cases.

Properties

Class Name Unchecked Parameter Dereference (C#)
Significance reliability
Mnemonic CSHARP.STRUCT.UPD
Categories
CWE CWE:476 NULL Pointer Dereference
Availability Available for C# only.
Enabling Checks for this warning class are enabled by default. To disable them, add the following WARNING_FILTER rule to the project configuration file.
WARNING_FILTER += discard class="Unchecked Parameter Dereference (C#)"

Example

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestFormalNullWarning
{
    public class TestFormalNullWarning
    {
        private int f;

        public TestFormalNullWarning(int f)
        {
            this.f = f;
        }

        public bool Equals(Object other)
        {
            return this.GetType() == other.GetType() && f == ((TestFormalNullWarning)other).f; // "Unchecked Parameter Dereference (C#)" warning issued here:
                                                                                               // - formal parameter 'other' is never checked for nullness
        }
    }
}

Resolution

Check if the warning corresponds to a situation where null might actually be dereferenced at runtime. If that is the case, add a nullness check for the value being dereferenced, or change the logic of the code. Sometimes, a warning of this checker corresponds to a spurious nullness check, that can be removed.

Relevant Configuration File Parameters

The following configuration file parameters affect checks for this warning class.