Buffer overflow occurs when a program attempts to write more data into a buffer—a contiguous block of memory allocated to store data—than it can hold. The extra data to overwrite adjacent memory locations, which may include critical variables, control data