Are functions like strcpy, gets, etc. always dangerous? What if I write a code like this:
int main(void)
{
char *str1 = \"abcdefghijklmnop\";
char *str2 = m
The only way malloc may fail is when an out-of-memory error occurs, which is a disaster by itself. You cannot reliably recover from it because virtually anything may trigger it again, and the OS is likely to kill your process anyway.
You are forcefully stuffing completely different things into one category.
Functions gets
is indeed always dangerous. There's no way to make a safe call to gets
regardless of what steps you are willing to take and how defensive you are willing to get.
Function strcpy
is perfectly safe if you are willing to take the [simple] necessary steps to make sure that your calls to strcpy
are safe.
That already puts gets
and strcpy
in vastly different categories, which have nothing in common with regard to safety.
The popular criticisms directed at safety aspects of strcpy
are based entirely on anecdotal social observations as opposed to formal facts, e.g. "programmers are lazy and incompetent, so don't let them use strcpy
". Taken in the context of C programming, this is, of course, utter nonsense. Following this logic we should also declare the division operator exactly as unsafe for exactly the same reasons.
In reality, there are no problems with strcpy
whatsoever. gets
, on the other hand, is a completely different story, as I said above.
Aside for potentially dereferencing NULL (as you do not check the result from malloc) which is UB and likely not a security threat, there is no potential security problem with this.