ABV.MEMBER

Buffer overflow-array index out of bounds in a structure

ABV.MEMBER checks for array bounds violations in a class or structure. The checker flags any code in which a buffer overflow will occur as a result of this situation. This checker has been extracted from checker ABV.GENERAL to find these specific buffer overflow situations separately, in case you habitually overflow the structure in your code, in which case you would want to turn this checker off.

Vulnerability and risk

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

Vulnerable code example

Copy
   typedef struct Data {
     int kind;
     char name[8];
     char ext[3];
   } Data;

   void resetDefaults(Data *d) 
   {
     d->kind = 0;
    memset(d->name, 0, 11);
   }

Klocwork produces an ABV.MEMBER report at line 10, indicating that array 'd->name' will overflow. If the design intention is to zero the name and extension, then this code isn't a problem. Otherwise, it's best to make the change shown in the following example.

Fixed code example

Copy
   typedef struct Data {
     int kind;
     char name[8];
     char ext[3];
   } Data;

   void resetDefaults(Data *d) 
   {
     d->kind = 0;
    memset(d->name, 0, 8);
    memset(d->ext, 0, 3);
  }

In the fixed code example, the size of the array has been changed so that array 'd->name' isn't used as an alias for both 'name' and 'ext'.

Security training

Application security training materials provided by Secure Code Warrior.

Extension

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