Start here

Home
About Klocwork
What's new
Fixed issues
Release notes
Installation

Reference

C/C++ checkers
Java checkers
C# checkers
MISRA C 2004 checkers
MISRA C++ 2008 checkers
MISRA C 2012 checkers
MISRA C 2012 checkers with Amendment 1
Commands
Metrics
Troubleshooting
Reference

Product components

C/C++ Integration build analysis
Java Integration build analysis
Desktop analysis
Refactoring
Klocwork Static Code Analysis
Klocwork Code Review
Structure101
Tuning
Custom checkers

Coding environments

Visual Studio
Eclipse for C/C++
Eclipse for Java
IntelliJ IDEA
Other

Administration

Project configuration
Build configuration
Administration
Analysis performance
Server performance
Security/permissions
Licensing
Klocwork Static Code Analysis Web API
Klocwork Code Review Web API

Community

View help online
Visit RogueWave.com
Klocwork Support
Rogue Wave Videos

Legal

Legal information

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.