Is the function strcpy always dangerous?

后端 未结 9 2375
不知归路
不知归路 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 08:56

    As you point out, under constrained circumstances strcpy isn't dangerous. It is more typical to take in a string parameter and copy it to a local buffer, which is when things can get dangerous and lead to a buffer overrun. Just remember to check your copy lengths before calling strcpy and null terminate the string afterward.

提交回复
热议问题