C#


CSHARP.NULL.RET.UNCHECKED : Call Might Return Null (C#)

要旨

The return value of a method is frequently checked against null, but not here.

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.

プロパティ

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

using System;
using System.Collections.Generic;

namespace DocumentationExample
{
    class CheckReturnedValue
    {

        Dictionary<string,string> dict;

        public CheckReturnedValue(Dictionary<string, string> dict)
        {
            this.dict =dict;
        }

        private Object Unknown()
        {
            string value = "";
            dict.TryGetValue("hello",out value);
            return value;
        }

        public void Test0()
        {
            Object o = Unknown();  // Call Might Return Null (C#) warning issued here 
                                   // - return value is checked every other time Unknown() is called, but not here 
            Console.WriteLine("test0: " + o.ToString());
        }

        public void Test1()
        {
            Object o = Unknown();
            if (o != null)
                Console.WriteLine("test1: " + o.ToString());
        }

        public void Test2()
        {
            Object o = Unknown();
            if (o != null)
                Console.WriteLine("test2: " + o.ToString());
        }

        public void Test3()
        {
            Object o = Unknown();
            if (o != null)
                Console.WriteLine("test3: " + o.ToString());
        }

        public void Test4()
        {
            Object o = Unknown();
            if (o != null)
                Console.WriteLine("test4: " + o.ToString());
        }

    }

}

解決法

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.

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

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