JavaScript is not currently enabled, but is required for full CodeSonar manual search and browse functionality.
If you are viewing this file in your hub's Web GUI, enable JavaScript in your browser: you will also need it for GUI functionality.
If you opened this file directly from disk, your browser may be directly suppressing JavaScript functionality: certain browsers perform this suppression on local files (but not files delivered by web servers) for security reasons.
| CodeSonar® 9.2p0 | CONFIDENTIAL | CodeSecure Inc |
The absolute value of a random number might actually be negative.
By computing the absolute value of a random integral number, one might actually yield a negative number, if Math.Abs() is used. For instance, Console.WriteLine(Math.Abs(Int32.MaxValue)) would actually print the negative value -2147483648. As a consequence, this might result in unexpected or erroneous computations.
| クラス名 | Abs on random (C#) | |||
|---|---|---|---|---|
| 日本語クラス名 | Abs on random (C#) | |||
| クラス分類 | 信頼性 (reliability) | |||
| ニーモニック | CSHARP.MATH.ABSRAND | |||
| カテゴリー |
|
|||
| 対応言語 | C# で利用可能です。 |
|||
| 有効/無効設定 | このワーニングクラスのチェックはデフォルトで有効になっています。チェックを無効にするにはプロジェクト設定ファイル
(configuration file)に以下の WARNING_FILTER ルールを追加してください。
WARNING_FILTER += discard class="Abs on random (C#)" |
using System;
namespace DocumentationExamples
{
public class AbsOfRandom
{
public static void Main(string[] args)
{
System.Random r = new System.Random();
int i = r.Next();
i = Math.Abs(i); // Abs on random (C#) warning issued here
Console.WriteLine(i);
}
}
}
In this example, the programmer should check for the minimum integral value explicitly and behave accordingly, as in the following example.
System.Random r = new System.Random();
int i = r.Next();
if (i == Int32.MinValue)
i = 0; // any non-negative value would do
else if (i < 0)
i = -i;
Console.WriteLine(i);
Check, explicitly, for the minimal integral value, before computing the absolute value. Otherwise, since Java 15, java.lang.Math.absExact and java.lang.StrictMath.absExact allows to compute the absolute values in a safe way, throwing ArithmeticException if the result overflows.
設定ファイルの以下のパラメータがこのワーニングクラスのチェックに影響します。
To report problems with this documentation, please visit https://support.codesecure.com/.