WEEK-3 Write a program to handle Array Indexing Errors.
Mon Mar 10 2025 18:10:37 GMT+0000 (Coordinated Universal Time)
Saved by
@aksharayadav
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h> // Include this for the bool type
int* IntVector;
void bar(void)
{
printf("Augh! I've been hacked!\n");
}
void InsertInt(unsigned long index, unsigned long value)
{
// Check for bounds before accessing the array
if (index >= 0xffff) {
printf("Index out of bounds!\n");
return;
}
printf("Writing memory at %p\n", &(IntVector[index]));
IntVector[index] = value;
}
bool InitVector(unsigned long size)
{
IntVector = (int*)malloc(sizeof(int) * size);
if (IntVector == NULL) {
return false;
}
printf("Address of IntVector is %p\n", IntVector);
return true;
}
int main(int argc, char* argv[])
{
unsigned long index, value;
if (argc != 3)
{
printf("Usage: %s [index] [value]\n", argv[0]);
return -1;
}
if (!InitVector(0xffff))
{
printf("Cannot initialize vector!\n");
}
return -1;
index = atol(argv[1]);
value = atol(argv[2]);
InsertInt(index, value);
// Free allocated memory
free(IntVector);
return 0;
}
content_copyCOPY
Comments