I am currently learning C and I am working on a task to improve some C-Code that might lead to a program crash.
Here is the code:
int main()
{
// Define buffers to store username and password
char username[16];
char password[16];
// Read username and password from user input
printf("Enter your name: ");
scanf("%s", username);
printf("Enter your password: ");
scanf("%s", password);
printf("[SHOUTING OUT LOUD] Hello, %s!\n", username);
return 0;
}
How can I make sure, that the input is not longer than 15 chars? Otherwise, the program could accidentally print out the password or overwrite the return address on the stack, which was subject of my other question:
Is it possible to crash this program somehow?
I already thought about putting the variables on the heap, but in the beginning, I don't know how long the input is. So I don't know, how much space I shall allocate.
Can somebody help me with this? Thanks :)