(Im running this on A VirtualBox
Ubuntu) So my question is: Why is it
possible to enter 11 chars into that 8
byte array?
11+1 for zero termination = 12 characters. IOW crash occurs when gets() writes 13 characters into the arr[8].
You haven't posted precise stack trace, but from my experience it should have crashed after foo()'s return.
Stack frame (with for void foo() + gets()) would look like (*):
- <lower memory address>
- gets() local variables
- saved stack pointer at the moment of gets() call (so called "prologue")
- return address, points to foo()
- foo() local variables (your char arr[8])
- saved stack pointer at the moment of gets() call
- return address, points to caller of foo()
- <higher memory address>
From all the information, the most important bits are the return address and saved stack pointer. And write of 13th byte in your case likely has corrupted the saved stack pointer of foo() function. Highly likely call of the following printf() would succeed, as the stack pointer is still valid (last changed by returning from gets()). But the returning from foo() would cause the foo()'s saved stack pointer (now corrupt) to be restored and then any action accessing stack from inside the calling function would go to a bad address.
From my experience this is the likeliest scenario. When stack is corrupt, it is really hard to tell for sure what would happen.
(*) For precise details how stack frame is constructed look for ABI - Application Binary Interface - for your architecture: for example IA-32 ABI for Intel i386 or AMD64 ABI for AMD64.