Java


JAVA.FUNCS.INFREC : Potential Infinite Recursion (Java)

要旨

A method call looks infinitely recursive.

This checker identifies very simple cases of infinite recusion, when a method calls itself with exactly the same parameter values. This leads very often to an infinite recursion and is likely to be a programming bug. Note, however, that, in reality, there might not be an actual infinite recursion if the state is modified by the method and termination depends on those changes of state. Although, in those cases, it could still be argued that the programmer is using a bad, unclear programming pattern and should at least clarify the code.

プロパティ

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

public class Loop {
  public static void main(String[] args) {
      System.out.println(new Loop().pow(args.length));
  }

  public int pow(int i) {
      int j = i;
      if (i == 0)
          return 1;
      else
          return 2 * pow(j); // "Potential Infinite Recursion (Java)" warning issued here
  }
}

The programmer probably forgot a -1 in the parameter of the recursive call. To resolve the issue, they should correct the recursive call in order to guarantee a decreasing chain of recursive parameter values.

  public int pow(int i) {
      int j = i;
      if (i == 0)
          return 1;
      else
          return 2 * pow(j - 1);
}        

解決法

Check if there is an actual possibility of non-termination and correct the recursion accordingly.

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

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