6
The program below allows the occurrence of memory overflow, as it is possible to overwrite the variable zero, placing a value "large" in the variable buffer
. How to make a safe program by avoiding the buffer overflow?
#include <stdio.h>
main(){
char buffer[8];
int zero = 0;
gets(buffer);
puts(buffer);
if(zero == 0){
printf("Zero continua sendo zero");
}else{
printf("A variavel zero foi modificada");
}
return 0;
}
Watch out! It’s not guaranteed that this program will work (it’s even weird that it actually works). You are assuming an order of variables in memory that is no guarantee of the language. The compiler is free to rearrange them.
– Kahler
@Kahler, thank you!
– Ed S