summaryrefslogtreecommitdiffstats
path: root/gdb/overwrite/overwrite.c
blob: 98b119ff59f318d4ea3b7c3e67b4461783df91df (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
 *  Simple test program to demonstrate memory overwrite.
 *
 *  $Id$
 */

#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>

typedef struct {
  uint32_t    TooShortArray[8];
  uint32_t    RandomVariable;
  uint32_t    CorruptedVariable;
} Struct_t;

Struct_t S;

void writeEntry(int index, uint32_t value);

int main(
  int    argc,
  char **argv
)
{
  puts( "\n\n*** MEMORY CORRUPTION EXAMPLE ***" );

  /* initialize global variables */
  memset( &S, 0, sizeof(S) );
  S.RandomVariable = 0x1234;
  S.CorruptedVariable = 0x5678;

  printf( 
    "&TooShortArray[0] = %p\n"
    "&TooShortArray[7] = %p\n"
    "&RandomVariable = %p\n"
    "&CorruptedVariable = %p\n",
    &S.TooShortArray[0],
    &S.TooShortArray[7],
    &S.RandomVariable,
    &S.CorruptedVariable
  );
  /* write past end of array */
  writeEntry( 8, 0xdeadf00d );
  writeEntry( 9, 0x0badd00d );

  printf( 
    "RandomVariable = 0x%08x\n"
    "CorruptedVariable = 0x%08x\n",
    S.RandomVariable,
    S.CorruptedVariable
  );
  puts( "*** END OF MEMORY CORRUPTION EXAMPLE ***" );
  exit( 0 );
}

void writeEntry(int index, uint32_t value)
{
  S.TooShortArray[index] = value;
}