C#


CSHARP.NULL.PARAM.ACTUAL : Null Parameter Dereference (C#)

要旨

An actual parameter passed to a method might be null.

A NullReferenceException could be triggered if a specified argument is null and the method/field (used through reflection) is an instance method/field . When this instructions are detected, CodeSonar tries to statically resolve the reflection, in order to check if it is a valid reflection or if it leads to a NullPointerException NullReferenceException. For performance reasons, CodeSonar checks only the constants inside the method where the reflection instruction is called.

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.

プロパティ

クラス名 Null Parameter Dereference (C#)
日本語クラス名 Null Parameter Dereference (C#)
クラス分類 信頼性 (reliability)
ニーモニック CSHARP.NULL.PARAM.ACTUAL
カテゴリー
CWE CWE:476 NULL Pointer Dereference
対応言語 C# で利用可能です。
有効/無効設定 このワーニングクラスのチェックはデフォルトで有効になっています。チェックを無効にするにはプロジェクト設定ファイル (configuration file)に以下の WARNING_FILTER ルールを追加してください。
WARNING_FILTER += discard class="Null Parameter Dereference (C#)"

using System;

namespace DocumentationExamples
{

    public class BasicNullness
    {

        public static void Main(string[] args)
        { }

        string f = null;
        public void Test1(string s)
        {
            if ((DateTime.Now.Millisecond % 2) == 0 || f != null)
                Console.WriteLine(f.Length);     // Null Pointer Dereference (C#) warning issued here 
                                                 //  - f is always null 
            if (s != null)
                Console.WriteLine("s01 is null");
            Console.WriteLine(s.Length);         // Null Pointer Dereference (C#) warning issued here 
                                                 // - Either the previous null-check of s is redundant (and should be removed)
                                                 //   or there should be one for this call too. 
        }
        public void Test2(string par)
        {
            string s = DateTime.Now.Millisecond % 2 == 0 ? null : "ciao";
            BasicNullnessSupport ts = new BasicNullnessSupport();
            int n01 = ts.M01(s);                 // Two Null Parameter Dereference (C#) warning instances issued here 
                                                 // - one because s may have been assigned null 
                                                 // - one because there is a subsequent null-check of s: either that check is redundant (and should be removed)
                                                 //    or there should be one for this call too. 
            int n02 = ts.M02(s);
            if (s != null)
                ts.M01(s);
            ts.M01(par);
            Console.WriteLine(n01 + n02);
        }
        public void Test3(string par)
        {
            if (par != null)
                Console.WriteLine("is non-null");
            new BasicNullnessSupport().M01(par); // Null Parameter Dereference (C#) warning issued here
                                                 // - either the previous null-check of s is redundant (and should be removed)
                                                 //   or there should be one for this call too.
        }
    }
    
    public class BasicNullnessSupport
    {
        public int M01(string s)
        {
            return s.Length;
        }
        public int M02(string s)
        {
            if (s != null)
                return s.Length;
            return -1;
        }
    }
}

解決法

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.

関連のある設定ファイルパラメータ

設定ファイルの以下のパラメータがこのワーニングクラスのチェックに影響します。