The function strncpy (3) can also be used to copy only a part of the source string into the destination.
函数strncpy(3)还可以用来仅把源字符串的一部分复制到目标中。
Both strncpy (3) and strncat (3) require that you give the amount of space left, not the total size of the buffer.
strncpy(3)和strncat(3)都要求您给出剩余的空间,而不是给出缓冲区的总大小。
The correct way to do this would be to use the strncpy function instead of strcpy , and give the maximum number of bytes to copy as BUFSIZ - 1.
复制的正确方法是用strncpy函数代替strcpy,并将要复制的最大字节数定为BUFSIZ-1。
The function strncpy(3) also doesn't NUL-terminate its destination if the source string is at least as long as the destination; this can cause havoc later.
如果源字符串至少和目标一样长,那么函数strncpy(3)还不会使用NUL 来结束字符串;这可能会在以后导致严重破坏。
This means that switching from strcpy (3) to strncpy (3) can reduce performance — often not a serious problem on today's computers, but it can still be a nuisance.
这意味着从strcpy(3)切换到strncpy(3)会降低性能——这在如今的计算机上通常不是一个严重的问题,但它仍然是有害的。
Memcpy joins the ranks of other popular functions like strcpy, strncpy, strcat, strncat which were banned due to their security vulnerability through buffer overruns.
memcpy加入到strcpy,strncpy,strcat,strncat等常用函数的队伍中,都是由于可通过缓存溢出进行安全攻击而被禁用。
Then, we'll discuss two examples of the static approach (standard C strncpy/strncat and OpenBSD's strlcpy/strlcat), followed by two examples of the dynamic approach (SafeStr and C++'s std::string).
然后,我们将讨论静态方法的两个例子(标准Cstrncpy/strncat和OpenBSD 的strlcpy/strlcat ),接着讨论动态方法的两个例子(SafeStr 和 C++ 的std::string )。
Then, we'll discuss two examples of the static approach (standard C strncpy/strncat and OpenBSD's strlcpy/strlcat), followed by two examples of the dynamic approach (SafeStr and C++'s std::string).
然后,我们将讨论静态方法的两个例子(标准Cstrncpy/strncat和OpenBSD 的strlcpy/strlcat ),接着讨论动态方法的两个例子(SafeStr 和 C++ 的std::string )。
应用推荐