You should examine disassembly. From there you will see what happens precisely.
Generally speaking, there are two things to consider:
Padding done by the compiler to align stack variables.
Relative placement of the stack variables by the compiler.
The first point: Your array char buffer[5];
will be padded so int admin;
will be properly aligned on stack. I would expect it to be generally padded to 8 bytes on both x86 or x64 and so 9 symbols to overwrite. But compiler might do differently depending on what it sees fit. Nonetheless, it appears that Windows and Linux machines are x86 (32bit).
The second point: compiler is not required to put stack variables on stack in order of their declaration. On Windows and first Linux machine compiler does indeed place char buffer[5];
below int admin;
, so you can overflow into it. On second Linux machine, compiler chooses to place it in reverse order, so instead of overflowing into int admin;
, you are corrupting stack frame of the caller of main()
after writing beyond space allocated for char buffer[5];
.
Here is shameless link to my own answer to a similar question - an example of examining of such overflow.