Is gcc reordering local variables at compilation time?
The compiler authors are completely free to implement any allocation scheme for local variables with automatic storage. auth_flag
could be set before or after password_buffer
on the stack, it could be in a register, it could be elided completely if proper analysis of the code allows it. There might not even be a stack... The only guarantee the Standard gives you is this:
strcpy(password_buffer, password);
invokes undefined behavior if the source string including its null terminator is longer than the destination array password_buffer
. Whether this undefined behavior fits your needs is completely outside of the language specification.
As a matter of fact, some implementors purposely complicate the task of would be hackers by randomizing the behavior in cases such as the posted code.