C CheckPointer Subobject Access Example

Here is some sample code that performs an out of bounds access in an array within a struct.


#include <stdio.h>
struct abc
{
  char a[8];
  char b[8];
};

int main()
{
  int index;
  struct abc test;
  /* out of bounds on a[]  */
  sprintf(test.a, "Too long"); /* note: copies 9 characters "into" test.a */
  /* another out of bounds access */
  printf("\nResult=%s\n\n", test.a);
  return 0;
}


CheckPointer detects sub-object out-of-bounds accesses. It also uses a special wrapper for sprintf (and other C runtime library functions) that allows it to detect the kind of error depicted by this example regardless of whether the program declares a global or local variable for the struct abc or allocates it on the heap.

In particular, for the given code fragment, CheckPointer reports:


  *** Error: Dereference of pointer is out of bounds.
    in wrapper function: sprintf
      called in function: main, line: 13, file: example.c

If you configure CheckPointer to not stop after the first error (with an assertion failure), it will continue program execution and provide additional error messages. In particular it reports:


  *** Error: Dereference of pointer is out of bounds.
    in wrapper function: printf
      called in function: main, line: 15, file: example.c

See a larger CheckPointer example that pinpoints a mysterious crash.

Semantic Designs also provides a variety of other tools.

For more information: info@semanticdesigns.com    Follow us at Twitter: @SemanticDesigns

C CheckPointer
Memory Safety Example