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

ABV.ITERATOR

Buffer overflow-array index out of bounds in an iteration

ABV.ITERATOR checks for out-of-bounds access to an array element using a pointer in an iteration through the array. The checker flags any code in which a buffer overflow could occur as a result of this situation.

Vulnerability and risk

Consequences of buffer overflow include valid data being overwritten and execution of arbitrary and potentially malicious code.

Vulnerable code example 1

1   int example1()
2   {
3       int a[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
4       int *p;
5       for (p = a ; p < a + 10; p++)
6       {
7           if (bar(*p) < 5)
8               break;
9       }
10       return *p;   // ABV.ITERATOR
11   }

Klocwork produces a buffer overflow report for line 10 indicating that pointer 'p' is used when its value may exceed the bounds of array 'a'. Pointer 'p' is assigned to 'a' and iterated at line 5. In this case, the iteration may cause array 'a' to overrun its limit of 10.

Fixed code example 1

1   int example1()
2   {
3       int a[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
4       int *p;
5       for (p = a ; p < a + 10; p++)
6       {
7           if (bar(*p) < 5)
8               break;
9       }
10      if (p < a + 10)
11        return *p;
12      return 0;
13   }

In the fixed code example, a check is made at line 10 to ensure that array 'a' can't overflow.

Vulnerable code example 2

1   int example2(int x)
2   {
3       int a[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
4       int *p = bar (a, x); //function returns a pointer to some element in "a"
5       if (p < a + 5)
6            return *p;
7   
8       return p[5]; // DEFECT: p[5] points to a[11] or beyond 
9   }

Klocwork produces an ABV.ITERATOR report at line 8, indicating that array 'a' will overflow after the iteration.

Fixed code example 2

1   int example2(int x)
2   {
3       int a[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
4       int *p = bar (a, x); //function returns a pointer to some element in "a"
5       if (p >= a + 5)
6            return *p;
7   
8       return p[5];  
9   }

In the fixed code example, the syntax is corrected in line 5, and the buffer overflow is avoided.

Extension

This checker can be extended through the Klocwork knowledge base. See Tuning C/C++ analysis for more information.