Example Program Code

// ConsoleEXE.cpp : Defines the entry point for the console
// application.

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>

const int ciSize = 5;

typedef struct SmallStruct
{
short shShort1;
short shShort2;
} SmallStruct;

typedef struct BigStruct
{
long lLong1;
long lLong2;
} BigStruct;

void ArrayOverrunCrash(void);
void OversizedCastCrash(void);
void PrematureDeleteCrash(void);

int main(int argc, char* argv[])
{
bool bExit = false;
char sInput[ciSize];

//========

printf("Welcome to the Crash Test Dummy Program.\n");

do
{
printf("\nEnter 'a' to crash by an array overrun.\n");
printf("Enter 'c' to crash by an oversized cast.\n");
printf("Enter 'd' to crash by a premature delete.\n");
printf("Enter 'x' to exit.>>");

scanf("%s", sInput);

switch (sInput[0])
{
case 'a':
case 'A':
ArrayOverrunCrash();
break;
case 'c':
case 'C':
OversizedCastCrash();
break;
case 'd':
case 'D':
PrematureDeleteCrash();
break;
case 'x':
case 'X':
bExit = true;
break;
default:;
// Do nothing
}
}
while (! bExit);

return 0;
}

void ArrayOverrunCrash(void)
{
char *sASCII = new char[ciSize];

for (int i = 0; i <= ciSize; i++)
{
// sASCII[ciSize] is outside allocated memory.
sASCII[i] = (char)rand();
}

delete [] sASCII;
}

void OversizedCastCrash(void)
{
SmallStruct *pSmallStruct = new SmallStruct;

pSmallStruct->shShort1 = 1;
pSmallStruct->shShort2 = 2;
BigStruct *pBigStruct = (BigStruct *)pSmallStruct;
pBigStruct->lLong1 = 1;

// lLong2 is outside memory allocated to pSmallStruct.
pBigStruct->lLong2 = 2;
delete pSmallStruct;
}

void PrematureDeleteCrash(void)
{
SmallStruct *pSmallStruct = new SmallStruct;
pSmallStruct->shShort1 = 1;
delete pSmallStruct;

// pSmallStruct points to deleted memory.
pSmallStruct->shShort2 = 2;
}

 
 
This Help system was created using a trial version of Adobe RoboHelp. Files generated with the trial version are intended for evaluation purposes only and may not be used or distributed for any other purpose.