Home | History | Annotate | Download | only in tests
      1 
      2 /* A simple test to demonstrate heap, stack, and global overrun
      3    detection. */
      4 
      5 #include <stdio.h>
      6 #include <stdlib.h>
      7 
      8 short ga[100];
      9 
     10 __attribute__((noinline))
     11 int addup_wrongly ( short* arr )
     12 {
     13    int sum = 0, i;
     14    for (i = 0; i <= 100; i++)
     15       sum += (int)arr[i];
     16    return sum;
     17 }
     18 
     19 __attribute__((noinline))
     20 int do_other_stuff ( void )
     21 {
     22    short la[100];
     23    return 123 + addup_wrongly(la);
     24 }
     25 
     26 __attribute__((noinline))
     27 int do_stupid_malloc_stuff ( void )
     28 {
     29    int sum = 0;
     30    unsigned char* duh = malloc(100 * sizeof(char));
     31    sum += duh[-1];
     32    free(duh);
     33    sum += duh[50];
     34    return sum;
     35 }
     36 
     37 int main ( void )
     38 {
     39    long s = addup_wrongly(ga);
     40    s += do_other_stuff();
     41    s += do_stupid_malloc_stuff();
     42    if (s == 123456789) {
     43       fprintf(stdout, "well, i never!\n");
     44    } else {
     45       fprintf(stdout, "boringly as expected\n");
     46    }
     47    return 0;
     48 }
     49