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

Infinite loop with local 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.LOCAL checker finds infinite loops whose exit conditions are based on a local variable.

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

1    void infinite_loop(int a)
2      {
3        int i=0;
4        while (true) {
5          if (i >= 10) {
6            if (a == 11) { 
7              break;
8            }
9          }
10         i++;
11       }
12     }

Klocwork flags this situation as an infinite loop, since, unless 'a' equals 11 on entry to the loop, it will never reach that value and so the loop will never reach a valid exit condition. In this case, the INFINITE_LOOP.LOCAL checker has found a typical infinite loop whose exit condition is based on a local variable.

Fixed code example 1

1    void infinite_loop(int a)
2      {
3        int i=0;
4        while (true) {
5          if (i >= 10) {
6            if (a == 11) { 
7              break;
8            } else {
9              a++;
10            }
11          }
12         i++;
13       }
14     }

In the fixed code example, 'a' is incremented, so that the function has an exit.

Vulnerable code example 2


1    void foo() {
2      int i=get();
3      int j=1;
4      while(1) {
5        if (i==1) {
6          j++;
7        } else {
8          break;
9        }
10      }
11    }

In this example, Klocwork flags an INFINITE_LOOP defect because 'i' is never changed. The function has no exit since its exit condition is invariant.

Fixed code example 2


1    void foo() {
2      int i=get();
3      int j=1;
4      while(1) {
5        if (i==1) {
6          i++;
7        } else {
8          break;
9        }
10      }
11    }

In the fixed code example, variable 'i' is incremented, changing the exit condition for the function.