This C-code compiles without any errors/warning. When I run this program, I can enter more than 16 chars and it will gladly echo all of my chars.
Forever?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFFER_SIZE 16
int main() {
char* buffer = malloc(BUFFER_SIZE);
while (1) {
bzero(buffer, BUFFER_SIZE);
scanf("%s", buffer);
puts(buffer); // echo
}
free(buffer);
return 1;
}
(Compile with: "gcc bufferoverflow.c -o buffer -Wall")
Why is this working? When will it crash?