strcat Vs strncat - When should which function be used?

后端 未结 5 893
无人及你
无人及你 2021-01-02 11:55

Some static code analyzer tools are suggesting that all strcat usage should be replaced with strncat for safety purpose?

In a program, if we know clearly the size of

5条回答
  •  滥情空心
    2021-01-02 12:20

    If you are absolutely sure about source buffer's size and that the source buffer contains a NULL-character terminating the string, then you can safely use strcat when the destination buffer is large enough.

    I still recommend using strncat and give it the size of the destination buffer - length of the destination string - 1

    Note: I edited this since comments noted that my previous answer was horribly wrong.

提交回复
热议问题