This is the line that can cause buffer overflow
buffer[i] = c;
since you are not checking whether i
is within bounds before that statement.
When that happens, you are most likely going to override the part of stack frame that contains code since there is no other function variable after buffer
.
I ran the program by piping the contents of a file that contains 100 A
s in it to the stdin
of the program. I got the following message, which confirmed my suspicion.
>> cat test-121.in | ./test-121
*** stack smashing detected ***: ./test-121 terminated
Aborted
I don't see how that could change the value of True
at all.
BTW, you have:
if (!True)
printf("%s: You are a hacker\n", name);
else
printf("%s: You are not a hacker\n", name);
did you mean if (True)
?? You have initialized True
to 0
.
Update
You asked: How can you not use a return value?
Answer: return
statement is optional for main
. Here's a section of the description of main from http://en.cppreference.com/w/cpp/language/main_function.
(4) The body of the main function does not need to contain the return statement: if control reaches the end of main without encountering a return statement, the effect is that of executing return 0;