Why is get () more dangerous than scanf ()?

It seems to me that both have the potential for buffer overflows. However, I am advised never to use gets (), but it is still recommended to use scanf ().

Is it just because of the formatting arguments allowed in scanf (), or is there another reason?

+5
source share
6 answers

The function is getsnot protected from buffer overflow.

Using the format string, scanfyou can determine the maximum length of the string for reading from standard input and saving in this memory buffer. For example, when scanf("%10s\n", str);no more than 10 characters are read. The buffer strmust be 11 bytes in order to preserve the terminating NULL character.

, scanf, gets, fgets.

+8

, , gets() . , gets() ( C11). , scanf() . , scanf() .

, fgets(), .

+2

. fgets , .

+1

gets . scanf .

, C?

+1

- scanf().

Google "scanf security" ~ 212k. Wikipedia, ,

%s .

, scanf . gets , scanf , gets - .

+1

gets() . , . gets() . , , gets() . - , . , . , , , execv(), . . gets() .

+1
source

All Articles