0
Considering this simple password validator in C:
#include <stdio.h>
#include <string.h>
int main(void)
{
char buff[5];
int pass = 0;
printf("\n Entre com a senha : \n");
gets(buff);
if (strcmp(buff, "1234"))
{
printf ("\n Senha Errada \n");
}
else
{
printf ("\n Senha Correta \n");
pass = 1;
}
if (pass) /* O usuário acertou a senha, poderá continuar*/
{
printf ("\n Acesso Liberado \n");
}
else
{
printf ("\n Acesso Negado \n");
}
return 0;
}
If I enter the following password:
123456
the exit is:
Senha Errada
Acesso Liberado
Why access is being released if the password is wrong?
I know that the way the code is written, the string 123456
bursts the vector preventing the correct functioning of the function strcmp()
because of the absence of '\0'
, but if the program prints Senha Errada
, also means that pass = 0
and access denied, no?
I did the debug in Codeblocks, for some reason, when I put the password 123456
the variable pass
is filled with garbage, in this case 54
.
My question is: why to burst the vector buff
the variable pass
is affected?
Good afternoon, Maniero. My doubt is not about using strcmp itself. I believe I used it correctly. The problem is when I place a string larger than the vector supports it is changing the variable pass. I debugged the codeblocks, for some reason, when I put the password "123456" the variable pass is filled by garbage, in this case "54". My question is: why when bursting the Buff vector the pass variable is affected?
– Gustavo Oliveira
I edited to put a link, this is another problem, I just realized now because the less readable form fooled me.
– Maniero
Regarding illegibility. Do you mean the question, the code, or both? I edited a little after reading some guides here in the OS, I tried to make it more readable. Thanks for the touch.
– Gustavo Oliveira
@Gustavooliveira the code, you do the execution in an unintuitive way, because the most common is you seek to do what is true before. The question is ok.
– Maniero