为 strncpy 创建包装器以插入终止 null

发布于 2024-07-29 16:20:12 字数 1178 浏览 10 评论 0原文

我决定为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

轻拂→两袖风尘 2024-08-05 16:20:12

您不需要检查源是否大于目标,只需始终将目标中的最后一个字符设置为“\0”即可。

You don't need to check that source is greater than destination just always make the last char in the dest a '\0'.

看轻我的陪伴 2024-08-05 16:20:12

在访问数组之前检查dest_size,否则你会遇到麻烦:

if (dest_size > 0) {
    dest[dest_size - 1] = '\0';
}

实际上,现在我想起来了,最好还是死掉:

if (dest_size == 0) {
    fputs(stderr, "strncpy(x,y,0)");
    exit(1);
}

否则,你会遇到与原来相同的问题strncpy(),如果 dest_size 为 0,则 dest 可能不会终止。

Check dest_size before accessing the array or you'll get into trouble:

if (dest_size > 0) {
    dest[dest_size - 1] = '\0';
}

Actually, now that I think of it, it's probably better to just die:

if (dest_size == 0) {
    fputs(stderr, "strncpy(x,y,0)");
    exit(1);
}

Otherwise, you'll have the same problem as with the original strncpy(), that dest may not be terminated if dest_size is 0.

长梦不多时 2024-08-05 16:20:12

您应该研究半标准 BSD 函数 strlcpy()strlcat()

您应该考虑 ISO TR24731 函数(例如 strncpy_s())是否合适并且在您需要时可用。

You should investigate the semi-standard BSD functions strlcpy() and strlcat().

You should consider whether the ISO TR24731 functions such as strncpy_s() are appropriate and available where you need them.

涫野音 2024-08-05 16:20:12

如果您对缓冲区的分配没有任何限制,则 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.

我不吻晚风 2024-08-05 16:20:12

如果 dest_size 是要复制的字符数,source_size 是源字符串中的字符数。
你应该尝试这个:


 size_t numchars = dest_size > source_size ? source_size : dest_size;
 strncpy(dest,source,numchars) ;
 dest[numchars] = 0 ;

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:


 size_t numchars = dest_size > source_size ? source_size : dest_size;
 strncpy(dest,source,numchars) ;
 dest[numchars] = 0 ;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文