gets() is risky to use!
Consider the below program.
void read() { char str[20]; gets(str); printf("%s", str); return; } |
chevron_right
filter_none
The code looks simple, it reads string from standard input and prints the entered string, but it suffers from Buffer Overflow as gets() doesn’t do any array bound testing. gets() keeps on reading until it sees a newline character.
To avoid Buffer Overflow, fgets() should be used instead of gets() as fgets() makes sure that not more than MAX_LIMIT characters are read.
#define MAX_LIMIT 20 void read() { char str[MAX_LIMIT]; fgets(str, MAX_LIMIT, stdin); printf("%s", str); getchar(); return; } |
chevron_right
filter_none
Please write comments if you find anything incorrect in the above article, or you want to share more information about the topic discussed above.
Recommended Posts:
- Python | Excel File Comparison
- Pattern Recognition | Phases and Activities
- C program to Insert an element in an Array
- Types of Literals in C/C++ with Examples
- Running Python script on GPU.
- asctime() and asctime_s() functions in C with Examples
- Multitape Nondeterministic Turing Machine simulator
- time.h header file in C with Examples
- LOB Rules and Restrictions
- ML | Training Image Classifier using Tensorflow Object Detection API
- __builtin_inf() functions of GCC compiler
- TensorFlow 2.0
- Data Structures and Algorithms Online Courses : Free and Paid
- Count substrings that contain all vowels | SET 2



