If I compile a code with gets
, the compiler outputs a warning saying that the function shouldn't be used:
<source>:5:13: warning: 'char* gets(char*)' is deprecated [-Wdeprecated-declarations]
gets(buf);
^
In file included from <source>:1:0:
/usr/include/stdio.h:577:14: note: declared here
extern char *gets (char *__s) __wur __attribute_deprecated__;
^
/tmp/ccWRqB1K.o: In function `main':
/home/ce/<source>:5: warning: the `gets' function is dangerous and should not be used.
However, if I replace gets
with scanf
and a %s
format string, it does not output such a warning:
int main()
{
char buf[10];
scanf("%s",buf);
}
If this program is compiled and I provide an input larger than 10 bytes, the program segfaults, and the buffer is overflown.
How can I understand this difference? Is scanf
considered safe?