为 strncpy 创建包装器以插入终止 null
我决定为 strncpy
制作一个包装器,因为我的源代码需要我进行大量字符串复制。 我想确保如果源等于或大于目标,则字符串终止。
这段代码将在生产中使用,所以我只是想看看使用这个包装器是否有任何潜在的危险。
我以前从未做过包装,所以我正在努力使其完美。
非常感谢您的任何建议,
/* Null terminate a string after coping */
char* strncpy_wrapper(char *dest, const char* source,
const size_t dest_size, const size_t source_size)
{
strncpy(dest, source, dest_size);
/*
* Compare the different length, if source is greater
* or equal to the destination terminate with a null.
*/
if(source_size >= dest_size)
{
dest[dest_size - 1] = '\0';
}
return dest;
}
====编辑更新====
/* Null terminate a string after coping */
char* strncpy_wrapper(char *dest, const char* source,
const size_t dest_size)
{
strncpy(dest, source, dest_size);
/*
* If the destination is greater than zero terminate with a null.
*/
if(dest_size > 0)
{
dest[dest_size - 1] = '\0';
}
else
{
dest[0] = '\0'; /* Return empty string if the destination is zero length */
}
return dest;
}
I have decided to make a wrapper for strncpy
as my source code requires me to do a lot of string copies. And I want to ensure that the string is terminated if the source is equal or greater than the destination.
This code will be used in production, so I just want to see if there are any potential dangers using this wrapper.
I have never done wrappers before so I am trying to make it perfect.
Many thanks for any advice,
/* Null terminate a string after coping */
char* strncpy_wrapper(char *dest, const char* source,
const size_t dest_size, const size_t source_size)
{
strncpy(dest, source, dest_size);
/*
* Compare the different length, if source is greater
* or equal to the destination terminate with a null.
*/
if(source_size >= dest_size)
{
dest[dest_size - 1] = '\0';
}
return dest;
}
==== Edited updated ====
/* Null terminate a string after coping */
char* strncpy_wrapper(char *dest, const char* source,
const size_t dest_size)
{
strncpy(dest, source, dest_size);
/*
* If the destination is greater than zero terminate with a null.
*/
if(dest_size > 0)
{
dest[dest_size - 1] = '\0';
}
else
{
dest[0] = '\0'; /* Return empty string if the destination is zero length */
}
return dest;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您不需要检查源是否大于目标,只需始终将目标中的最后一个字符设置为“\0”即可。
You don't need to check that source is greater than destination just always make the last char in the dest a '\0'.
在访问数组之前检查dest_size,否则你会遇到麻烦:
实际上,现在我想起来了,最好还是死掉:
否则,你会遇到与原来相同的问题strncpy(),如果 dest_size 为 0,则 dest 可能不会终止。
Check dest_size before accessing the array or you'll get into trouble:
Actually, now that I think of it, it's probably better to just die:
Otherwise, you'll have the same problem as with the original strncpy(), that dest may not be terminated if dest_size is 0.
您应该研究半标准 BSD 函数
strlcpy()
和strlcat()
。您应该考虑 ISO TR24731 函数(例如
strncpy_s()
)是否合适并且在您需要时可用。You should investigate the semi-standard BSD functions
strlcpy()
andstrlcat()
.You should consider whether the ISO TR24731 functions such as
strncpy_s()
are appropriate and available where you need them.如果您对缓冲区的分配没有任何限制,则
strndup
可能更合适。它将分配和复制最多
len
个字符,并始终以 NULL 终止。If you don't have any constraints on the allocation of the buffer,
strndup
could be more appropriate.It will allocate and copy at most
len
chars, and always terminate with NULL.如果 dest_size 是要复制的字符数,source_size 是源字符串中的字符数。
你应该尝试这个:
if dest_size is the number of characters to be copied and source_size is the number of characters in source string.
you should try this: