C#


CSHARP.HARDCODED.FNAME : Hardcoded Filename (C#)

要旨

A file name is provided as a hardcoded string.

This checker identifies problems with the use of the external resources of the application. These are resource files, scripts or XML configuration files.

プロパティ

クラス名 Hardcoded Filename (C#)
日本語クラス名 Hardcoded Filename (C#)
クラス分類 信頼性 (reliability)
ニーモニック CSHARP.HARDCODED.FNAME
カテゴリー
CWE CWE:547 Use of Hard-coded, Security-relevant Constants
対応言語 C# で利用可能です。
有効/無効設定 このワーニングクラスのチェックはデフォルトで有効になっています。チェックを無効にするにはプロジェクト設定ファイル (configuration file)に以下の WARNING_FILTER ルールを追加してください。
WARNING_FILTER += discard class="Hardcoded Filename (C#)"

using System.IO;

namespace DocumentationExamples
{
    public class Resources
    {
        public static void Main(string[] args)
        {
            File.Create("hello.txt");                                       // "Hardcoded Filename (C#)" warning issued here
            new FileStream("C:\\myfile.txt", FileMode.OpenOrCreate);        // "Hardcoded Filename (C#)" warning issued here
            new FileStream("/home/user/myfile.bmp", FileMode.OpenOrCreate); // "Hardcoded Filename (C#)" warning issued here
            if (args.Length " 0)
                File.Create(args[0]);
        }
    }
}

In this example, the programmer should move the file names into configuration files, so that they can be modified and swapped for distinct operating systems.

Note also that two of the hardcoded file names are OS-dependent, so the program will not work consistently across distinct operating systems. The programmer could address this by using Path.DirectorySeparatorChar() to split path components in a OS-independent way.

解決法

Verify if the problem is real and the resource is actually used in an incorrect way. Avoid hardcoded file names: move them to a configuration file. Avoid OS-dependent resources and resource names.

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

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