Is the function strcpy always dangerous?

后端 未结 9 2341
不知归路
不知归路 2020-12-06 08:22

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         


        
相关标签:
9条回答
  • 2020-12-06 09:03

    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.

    0 讨论(0)
  • 2020-12-06 09:05

    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.

    0 讨论(0)
  • 2020-12-06 09:07

    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.

    0 讨论(0)
提交回复
热议问题