I have the following code:
_Bool grantAccess(char *password){
char goodPassWord[]= "goodpass";
return (0 == strcmp(password, goodPassWord));
}
_Bool grantAccessExercise(void){
char password[9];
int allow = 0;
printf("Please enter password: ");
gets(password);
if (grantAccess(password)) {
allow = 1;
}
return (allow != 0);
}
When I enter any combination of 10 characters for password it overflows and overwrites the null-terminator. Can anyone explain why the non null-terminated value causes StrCmp to return 0?
Can anyone explain why the non null-terminated value causes StrCmp to return 0?
This is not what happens.
What happens is:
password
overwrites bytes that are part of the stack-located variable allow
allow
does no longer contain the value zero, but some other value.allow
is not modified.allow
contains the non-zero value due to the overflow.In order to verify that, I made a test as follows:
allow
== 57, which is the ASCII code of character '9'.