An uninitialized variable is a variable that is declared but not assigned a value. In C, an uninitialized variable inherits the value of whatever was previously stored at that specific memory location.
If a function declares and initializes a variable, and a subsequent function call uses the same memory address for a new (uninitialized) variable, the new variable will contain the old value. This behavior can be exploited in security contexts (e.g., during reads or comparisons) or can lead to unpredictable logic bugs.
#include <stdio.h>
void trashed(void)
{
int x = 0xfacade;
printf("Integer 0 Declared at:\t%p\n", &x);
printf("Integer 0 Value:\t\t0x%x\n\n", x);
}
void scatterd(void)
{
int y;
printf("Integer 1 Declared at:\t%p\n", &y);
printf("Integer 1 Value:\t\t0x%x\n\n", y);
if (y == 0xfacade)
{
puts("Play your game, and walk away.\n");
}
}
int main()
{
// ... execution flow ...
trashed();
scatterd();
}