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
C#


CSHARP.FUNCS.INFREC : Potential Infinite Recursion (C#)

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 (C#)
Significance reliability
Mnemonic CSHARP.FUNCS.INFREC
Categories
CWE CWE:674 Uncontrolled Recursion
Availability Available for C# only.
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 (C#)"

Example

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); // "Potential Infinite Recursion (C#)" warning issued here
        }
    }
}

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

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/.