安全的strlcpy和strlcat字符串操作函数

本文探讨了传统字符串函数strcpy和strcat的安全问题,介绍了strncpy和strncat的局限性,并详细解析了strlcpy和strlcat如何有效解决这些问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

学过C语言的肯定都知道strcpy和strcat,但是这两个函数有个致命的缺陷,它们不检查dst是否有足够的空间,如果src足够长必然会导致缓冲区溢出,于是有就


了改进版strncpy和strncat,这两个函数在一定程度上解决了安全问题,但是很多程序员都不愿使用它们,原因如下:


1. 对'\0'的处理


size_t num参数表示需要拷贝的字符个数,在num小于等于src的情况下,strncpy只拷贝前num个字符,并不自动添加\0,如果dst没有全部初始化为\0,输出


就会乱码;在num大于src并且src等于dst的情况下,本来strncpy会自动在dst中添加\0,但此时dst空间已经满了,无法再拷贝\0,因此输出也会乱码。


2. 效率问题

另外,假如dst空间足够大,在src远小于dst的情况下,使用sizeof(dst)作为num参数进行拷贝时,会多拷贝dst-src-1次\0,这是完全不需要的操作,因此有


人说strncpy的有些行为是很诡异的。


因此使用strncpy时应该显式设置\0,标准方法是这样的:


1
2
strncpy(dst, src, sizeof(dst) - 1);
dst[sizeof(dst) - 1] = '\0';       // 手动添加\0

有人说这样写可以避免效率问题:

1
2
strncpy(dst, src, strlen(src));
dst[strlen(src)] = '\0';       // 手动添加\0

但其实回到问题的初衷了,原因是从src计算出的字串长度无法保证小于等于dst,可能会导致缓冲区溢出,因此依然无法避免多次拷贝\0的效率问题,strlcpy和


strlcat的出现很好的解决了上述两个问题。

1
2
size_t strlcpy(char *dst,const char *src,size_t siz);
size_t strlcat(char *dst,const char *src,size_t siz);

strlcpy的源码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/*
 * Copy src to string dst of size siz.  At most siz-1 characters
 * will be copied.  Always NUL terminates (unless siz == 0).
 * Returns strlen(src); if retval >= siz, truncation occurred.
 */
size_t
strlcpy(char *dst,const char *src,size_t siz)
{
    register char *d = dst;
    register const char *s = src;
    register size_t n = siz;
 
    if (s == 0 || d == 0) return 0;
 
    /* Copy as many bytes as will fit */
    if (n != 0 && --n != 0) {
        do {
            if ((*d++ = *s++) == 0)
                break;
        }while (--n != 0);
    }
 
    /* Not enough room in dst, add NUL and traverse rest of src */
    if (n == 0) {
        if (siz != 0)
            *d = '\0';     /* NUL-terminate dst */
        while (*s++)
            ;
    }
 
    return(s - src - 1);    /* count does not include NUL */
}

1. strlcpy可以自动处理\0,只需要将sizeof(dst)作为size参数即可。


2. strlcpy返回strlen(src),用于判断src是否被截断。

1
2
3
4
int len;
len = strlcpy(dst, src, sizeof(dst));
if (len >= sizeof(dst))
    printf("truncation occurred.");

strlcat的源码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/*
 * Appends src to string dst of size siz (unlike strncat, siz is the
 * full size of dst, not space left).  At most siz-1 characters
 * will be copied.  Always NUL terminates (unless siz <= strlen(dst)).
 * Returns strlen(src) + MIN(siz, strlen(initial dst)).
 * If retval >= siz, truncation occurred.
 */
size_t
strlcat(char *dst,const char *src,size_t siz)
{
    register char *d = dst;
    register const char *s = src;
    register size_t n = siz;
    size_t dlen;
 
    if (s == 0 || d == 0) return 0;
 
    /* Find the end of dst and adjust bytes left but don't go past end */
    while (n-- != 0 && *d != '\0')
        d++;
    dlen = d - dst;
    n = siz - dlen;
 
    if (n == 0)
        return(dlen + strlen(s));
    while (*s != '\0') {
        if (n != 1) {
            *d++ = *s;
            n--;
        }
        s++;
    }
    *d = '\0';
 
    return(dlen + (s - src));   /* count does not include NUL */
}

ps:strlcpy并不属于ANSI C,至今也还不是标准。不过glibc加入了strlcpy函数,目前Linux发行版中都有该函数。Windows下是没有strlcpy的,strcpy的安


全版本为strcpy_s,具体查询MSDN。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值