A preliminary study on how to overcome the gets function defect in C Language

Source: Internet
Author: User

1. gets function Introduction
The gets function is a C function used to read strings from the stdin stream. The gets function receives the characters entered by the user from the keyboard until it is terminated when a carriage return occurs. The prototype is:
Char * gets (char * buff );
Example:
# Include <stdio. h>
Int main ()
{
Charstr [30];
While (! Str! = Gets (str ));
Printf ("% s \ n", str );
Return 0;
}
If the read is successful, the gets function returns a pointer with the same value as str. Otherwise, a null pointer is returned.
2. gets Function Vulnerability
The gets function is a dangerous function, because the number of characters entered on the keyboard may exceed the maximum limit of the buf, And the gets function does not check it. For example, in the preceding example, if you enter 1111111111111111 on the keyboard ................ If the number of input 1 is greater than 30, the program will crash.
3. gets Function Vulnerability Solution
We can rewrite a new function Gets.
The prototype is char * Gets (int maxlen)
This function allows the programmer to specify the maximum number of input characters and allocate storage space for the characters in the function. The function returns char *
This function is designed for dynamic arrays, such
Int main ()
{
Char * p;
P = Gets (18 );
}
The Gets function parameter discards the passed pointer because the pointer to the passed function may be unreliable and cause program crash, such as passing in a wild pointer.
Another prototype of the Gets function is
Char * const Gets (char * const array, int maxlen );
This function is used to input a fixed-length array of characters, such
Int main ()
{
Charstr [20];
Gets (str, 20 );
Return 0;
}
In this case, one of the Gets function parameters is of the char * const type because we allow the programmer to modify the content pointed to by this type pointer instead of the pointer itself, this is the same as str [0] = '1' in char str [30], but it cannot be the same as str ++.
The specific implementation code is as follows:
# Include <string. h>
# Include <stdio. h>
# Include <conio. h>
# Include <stdlib. h>
# Include <io. h>
Char * Gets (int maxlen)
// A maximum of maxlen characters can be read from the keyboard, and the string pointer type is returned.
{
Int I;
Staticchar * str;
Char c;
Str = (char *) malloc (sizeof (char) * maxlen );
If (! Str)
{
Perror ("memeoryallocation error! \ N ");
Return0;
}
Else
{
For (I = 0; I <maxlen; I ++)
{
C = getchar ();
If (c! = '\ N') str [I] = c;
Elsebreak;
}
Str [I] = '\ 0 ';
Returnstr;
}
}
 
Char * const Gets (char * const array, int maxlen)
{
Int I;
Char c;
For (I = 0; I <maxlen; I ++)
{
C = getchar ();
If (c! = '\ N') array [I] = c;
Elsebreak;
}
Array [I] = '\ 0 ';
Returnarray;
}
 
Int main ()
{
Char s [8];
Gets (s, 8 );
Puts (s );
Fflush (stdin); // refresh the input buffer, which is very important; otherwise, the next Gets function will be affected.
Char * p = Gets (8 );
Puts (p );
Return 0;
}
Author: wangzhicheng2013

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.