Checker InfiniteRecursion

belongs to group Basic
Identify calls that look like infinite recursion

Frameworks supported by this checker

  • java up to 11
  • android up to API level 28
  • dotnet

Warnings generated by this checker

  • InfiniteRecursionWarning: a method call looks infinitely recursive [ CWE674 ]

Options accepted by this checker

  • none

Annotations understood by this checker

  • none


Description

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.

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

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.

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

Examples

Consider the following program:

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);
  }
}

This checker issues the following warning:

Loop.java:11: [InfiniteRecursion: InfiniteRecursionWarning] This call seems to induce an infinite recursion

since pow calls itself recursively with exactly the same parameter values, which leads to non-termination. The programmer probably forgot a -1 in the parameter of the recursive call.

In this example, the programmer 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);
  }

Consider the following program:

using System;

namespace DocumentationExamples
{

    public class InfiniteRecursion
    {
        public static void Main(string[] args)
        {
            Console.WriteLine(new InfiniteRecursion().Pow(args.Length));
        }
        public int Pow(int i)
        {
            int j = i;
            if (i == 0)
                return 1;
            else
                return 2 * Pow(j);
        }
    }
}

This checker issues the following warning:

DocumentationExamples.cs:18: [InfiniteRecursion: InfiniteRecursionWarning] This call seems to induce an infinite recursion

since Pow calls itself recursively with exactly the same parameter values, which leads to non-termination. The programmer probably forgot a -1 in the parameter of the recursive call.

In this example, the programmer 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);
}