12 questions in C language, involving pointers, processes, operations, struct, functions, and memory. Let's see how many questions you can make !, Question pointer
1. gets ()Function
Q: Please find out the problem in the following code:
# Include <stdio. h>
Int main (void)
{
Char buff [10];
Memset (buff, 0, sizeof (buff ));
Gets (buff );
Printf ("\ n The buffer entered is [% s] \ n", buff );
Return 0;
}
A: The problem in the code above lies in the use of the function gets (). This function receives a string from stdin without checking the cache volume it copies, which may cause cache overflow. We recommend that you use the standard function fgets.
2. strcpy ()Function
Q: The following is a simple password protection function. Can you crack the password without knowing it?
# Include <stdio. h>
Int main (int argc, char * argv [])
{
Int flag = 0;
Char passwd [10];
Memset (passwd, 0, sizeof (passwd ));
Strcpy (passwd, argv [1]);
If (0 = strcmp ("LinuxGeek", passwd ))
{
Flag = 1;
}
If (flag)
{
Printf ("\ n Password cracked \ n ");
}
Else
{
Printf ("\ n Incorrect passwd \ n ");
}
Return 0;
}
A: The key to cracking the preceding encryption is to use the strcpy () function. Therefore, when users Input random passwords to the passwd cache, they do not check whether the capacity of passwd is sufficient in advance. Therefore, if you enter a long "password" that is sufficient to cause cache overflow and overwrite the default value of the "flag" variable, even if the password cannot pass verification, the flag verification bit becomes non-zero, so that the protected data can be obtained. For example:
$./Psswd aaaaaaaaaaaaa
Password cracked
Although the above password is incorrect, we can still bypass password security protection through cache overflow.
To avoid this problem, we recommend using the strncpy () function.
Note: The latest compiler detects the possibility of stack overflow internally, so it is difficult to store variables in the stack to cause stack overflow. This is the case by default in my gcc, so I have to use the compile command '-fno-stack-protector' to implement the above scheme.
3. main ()Return type
Q: Can the following code be compiled? If yes, is there any potential problems?
# Include <stdio. h>
Void main (void)
{
Char * ptr = (char *) malloc (10 );
If (NULL = ptr)
{
Printf ("\ n Malloc failed \ n ");
Return;
}
Else
{
// Do some processing
Free (ptr );
}
Return;
}
A: Because of the return type of the main () method, errors in this Code are considered as warnings in most compilers. The return type of main () should be "int" rather than "void ". Because the "int" return type causes the program to return the status value. This is very important, especially when a program is running as part of a script dependent on the successful running of the program.
4.Memory leakage
Q: Will the following code cause memory leakage?
# Include <stdio. h>
Void main (void)
{
Char * ptr = (char *) malloc (10 );
If (NULL = ptr)
{
Printf ("\ n Malloc failed \ n ");
Return;
}
Else
{
// Do some processing
}
Return;
}
A: Although the above Code does not release the memory allocated to the "ptr", it does not cause memory leakage after the program exits. After the program ends, all the memory allocated by the program will be automatically processed. However, if the above Code is in a "while loop", it will lead to a serious memory leakage problem!
Tip: If you want to know