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.FUNCS.INFREC : Potential Infinite Recursion (Java)

Summary

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.

Properties

Class Name Potential Infinite Recursion (Java)
Significance reliability
Mnemonic JAVA.FUNCS.INFREC
Categories
CWE CWE:674 Uncontrolled Recursion
Availability Available for Java and Kotlin.
Enabling Checks for this warning class are enabled by default. To disable them, add the following WARNING_FILTER rule to the project configuration file.
WARNING_FILTER += discard class="Potential Infinite Recursion (Java)"

Example

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

Resolution

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

Relevant Configuration File Parameters

The following configuration file parameters affect checks for this warning class.

 

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