问题
I am currently learning C in one of my courses at the university. Now we have a task to solve and I am not sure how to do it.
The task looks like this: "Is it possible to let this program crash with user inputs? If so, explain this case."
The program we have been given is quite simple and looks like this:
#include <stdio.h> // Include to use printf, scanf
int main()
{
// Define buffers to store user name 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;
}
I already found out, that you can make the program print out the password, if you use a username longer than 15 chars. But this is obviously not a crash. So I haven't found a way to crash the program, but somehow I am pretty sure, that there is a way to do this. Does somebody has any idea?
Thanks :)
回答1:
Entering a username or password longer than 15 characters can crash the program, it's just not guaranteed to do so.
When you write past the bounds of an array, you invokes undefined behavior. Loosely speaking, that means you can't make any assumptions about what your program will do. It may crash, it may output strange results, or it may appear to work properly.
Just because the program could crash doesn't necessarily mean it will.
That being said, given the way most compilers you're likely to come across work, you're more likely to get a crash the longer the string you enter.
Local variables are typically allocated on the stack adjacent to each other. For example, suppose username
comes immediately before password
on the stack. If you enter in a 20 character name for the username, it will write past username
into password
and username
will not contain a null terminating byte. When you then enter a password, it will overwrite any characters of the username after the first 16. Then when you print username
, you'll see the first 16 characters of what you entered followed by the password.
Now suppose you enter in 100 characters for the username. This will write past username
and past password
and will most likely overwrite the return address for main
. Then when main
attempts to return is reads a bogus address, and attempting to jump to that address is what causes the crash.
But again, this is all very system specific, and can vary based on the type/order you place variables, which functions you call, and what optimization setting you use to compile, among others.
回答2:
It won't necessarily crash if you give an input of more than 15 characters in every environment. It depends on whether it uses an memory on the system that's inaccessible to it.
Undefined Behavior
If the contiguous areas of memory following the memory assigned to your string are empty or are currently unused/unclaimed by any other process at the time you run your program, then writing to them will show no effect.
But when you run it some other time, it is possible that the memory location following the allocated area is occupied/in use by some other process/program, which may crash your program.
Since, this behavior is unpredictable, it is known as undefined behavior.
Remember, C doesn't do out of memory bounds checking which is why it runs fine. But your program may crash anytime when you give input > 15 characters. In other programming languages, with strict checking, you will probably get an exception in this case.
Hope this helps !
Note : On windows, ctrl+C is your best shot if you are willing to consider this as crash. Rest all times, it will be indeterminable if your program would crash.
回答3:
But this is obviously not a crash. So I haven't found a way to crash the program, but somehow I am pretty sure, that there is a way to do this. Does somebody has any idea?
Well, if you really need a 'full crash' try:
MyProg < SomeLargeFile
来源:https://stackoverflow.com/questions/62214884/is-it-possible-to-crash-this-program-somehow