INFINITE_LOOP.MACRO

Infinite loop in macro

The INFINITE_LOOP checkers find instances of loops that have no exit. The checkers track the variables that control loop exit, flagging a defect if the conditions are invariant. When the validity of a condition doesn't change from one iteration to another, the loop never terminates.

The INFINITE_LOOP.MACRO checker finds infinite loops contained in macros. The typical use for the INFINITE_LOOP.MACRO checker is to separate the check for this type of infinite loop from those whose exit condition is based on a local variable. It is useful to be able to turn off the INFINITE_LOOP.MACRO checker when you want to check for significant infinite loops with INFINITE_LOOP.LOCAL.

Vulnerability and risk

An infinite loop can cause a program to hang up indefinitely or crash, possibly allowing a denial-of-service (DoS) attack due to excessive use of CPU or memory resources.

Vulnerable code example

Copy
    #define HALT() do { } while (1)
    void infinite_loop() {
       HALT();
    }

Klocwork flags this example of an infinite loop, since the function will never exit. In this case, the loop is contained in the macro HALT, which has been placed in the code to cause a halt. This is an example of a situation that you typically wouldn't want to flag when searching for infinite loops, and INFINITE_LOOP.MACRO lets you separate it from the type of code defect you do want flagged.