Consider the below program.
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.
Please write comments if you find anything incorrect in the above article, or you want to share more information about the topic discussed above.
- What's difference between char s and char *s in C?
- Operators in C | Set 1 (Arithmetic Operators)
- Difference between getc(), getchar(), getch() and getche()
- What is the difference between printf, sprintf and fprintf?
- How to dynamically allocate a 2D array in C?
- Difference between pointer and array in C?
- Sequence Points in C | Set 1
- Scansets in C
- puts() vs printf() for printing a string
- How to Count Variable Numbers of Arguments in C?
- What is use of %n in printf() ?
- Do not use sizeof for array parameters
- C function to Swap strings
- Storage for Strings in C
- How to print % using printf()?