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
Java


JAVA.LIB.RAND.NEW : Single-use Random Number Generator (Java)

要旨

A random number generator is recreated each time a random number is needed.

Instead, create only one instance of a static secure random generator and use it for all random number generation.

プロパティ

クラス名 Single-use Random Number Generator (Java)
日本語クラス名 Single-use Random Number Generator (Java)
クラス分類 信頼性 (reliability)
ニーモニック JAVA.LIB.RAND.NEW
カテゴリー
CWE CWE:1176 Inefficient CPU Computation
対応言語 Available for Java and Kotlin.
有効/無効設定 このワーニングクラスのチェックはデフォルトで有効になっています。チェックを無効にするにはプロジェクト設定ファイル (configuration file)に以下の WARNING_FILTER ルールを追加してください。
WARNING_FILTER += discard class="Single-use Random Number Generator (Java)"

import java.util.Random;
import java.security.SecureRandom;

public class Main {
  public static void main(String[] args) {
      Random r = new Random();                                    // Insecure Random Number Generator (Java) warning issued here
      int[] array = mkRandomArray(Math.abs(r.nextInt() % 1000));  // Single-Use Random Number Generator (Java) warning issued here
      for (int i: array)
        System.out.println(i);
      Random sr = new SecureRandom(new byte[]{1,1,1,1});          // Hardcoded Random Seed (Java) warning issued here
  }

  private static int[] mkRandomArray(int length) {
    int[] result = new int[length];
    for (int pos = 0; pos < length; pos++)
      result[pos] = new Random().nextInt();                       /* Warnings of two classes issued here:
                                                                   * - Insecure Random Number Generator (Java)
                                                                   * - Single-Use Random Number Generator (Java)
                                                                   */
    return result;
  }
}

To resolve these issues, the program could be modified as follows.

import java.security.SecureRandom;
import java.util.Random;

public class Main {
  private final static Random r = new SecureRandom();

  public static void main(String[] args) {
      int[] array = mkRandomArray(Math.abs(r.nextInt() % 1000));
      for (int i: array)
          System.out.println(i);
      Random sr = new SecureRandom();
  }

  private static int[] mkRandomArray(int length) {
      int[] result = new int[length];
      for (int pos = 0; pos < length; pos++)
          result[pos] = r.nextInt();
      return result;
  }
}

解決法

Use java.security.SecureRandom instead of java.util.Random. Store the random generator in a field instead of a local variable.

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

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

 

To report problems with this documentation, please visit https://support.codesecure.com/.