INFINITE_LOOP.GLOBAL

Infinite loop with global variable

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.GLOBAL checker finds infinite loops whose exit conditions are based on a global variable that isn't changed in the local loop. The typical use for the INFINITE_LOOP.GLOBAL 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.GLOBAL 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


1    int global_var;
2    void infinite_loop() {
3       while (1) {
4          if (global_var==0)
5            return;
6       }
7    
} 

Klocwork flags this example of an infinite loop, since the variable 'global' will never equal 0, and the function will never exit. In this case, the exit is based on variable 'global_var', which has been defined as a global variable outside the function 'infinite_loop' and is never changed in the loop. This is an example of a situation that you typically wouldn't want to flag when searching for infinite loops, and INFINITE_LOOP.GLOBAL lets you separate it from the type of code defect you do want flagged.