(standard c libraries translation )strcat

本文详细介绍了C语言中用于连接字符串的strcat和strncat函数,包括它们的使用方法、注意事项以及可能出现的问题,如缓冲区溢出。并通过示例代码展示了如何正确使用这些函数,并解释了strlcat函数如何解决strcat函数的潜在安全风险。

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

strcat, strncat - concatenate two strings
strcat,strncat-连接两个字符串

所需头文件
#include <string.h>
char *strcat(char *dest, const char *src);
char *strncat(char *dest, const char *src, size_t n);

The  strcat() function appends the src string to the dest string, overwriting the terminating null byte ('\0') at the end of dest, and then adds a terminating null byte.  The strings may not overlap, and the dest string must have enough space for the result.  If dest  is  not  large  enough,  program behavior is unpredictable; buffer overruns are a favorite avenue for attacking secure programs.
strcat()函数把src字符串附加到dest字符串,会覆盖掉dest尾部的字符串结束符\0,然后在结尾加上一个字符串结束符,字符串不能重叠,dest字符串必须有足够的空间来容纳结果,如果不够的话,程序行为是不可预测的,对于攻击安全的程序来说,缓冲区溢出是一个很好的途径

The strncat() function is similar, except that
       *  it will use at most n bytes from src; and
       *  src does not need to be null-terminated if it contains n or more bytes.
As with strcat(), the resulting string in dest is always null-terminated. If  src  contains n or more bytes, strncat() writes n+1 bytes to dest (n from src plus the terminating null byte).  Therefore, the size of dest must be at least strlen(dest)+n+1.
strncat函数类似,除了:
    1,最多使用src的n个字节
    2,src不需要字符结束符,如果src包含n或者更多字符。
相对于strcat(),结果是null-terminated的,如果src包含n或者更多字节,strncat会写n+1个字节到dest(src里面的n个字节和一个字符结束符),因此dest的大小至少是strlen(dest)+n+1

       A simple implementation of strncat() might be:
           char*
           strncat(char *dest, const char *src, size_t n)
           {
               size_t dest_len = strlen(dest);
               size_t i;

               for (i = 0 ; i < n && src[i] != '\0' ; i++)
                   dest[dest_len + i] = src[i];
               dest[dest_len + i] = '\0';

               return dest;
           }


The strcat() and strncat() functions return a pointer to the resulting string dest. Some systems (the BSDs, Solaris, and others) provide the following function:
strcat()和strncat()函数返回一个指向结果字符串dest的指针,一些系统(例如BSDs,Solaris和其他)提供如下的函数:
    size_t strlcat(char *dest, const char *src, size_t size);
    
his function appends the null-terminated string src to the string dest, copying at most size-strlen(dest)-1 from src, and adds a  null  terminator  to the  result,  unless  size is less than strlen(dest).  This function fixes the buffer overrun problem of strcat(), but the caller must still handle the possibility of data loss if size is too small.  The function returns the length of the string strlcat() tried to create; if the return value is greater than  or  equal  to  size,  data loss occurred.  If data loss matters, the caller must either check the arguments before the call, or test the function return value.  strlcat() is not present in glibc and is not standardized by POSIX, but is available on Linux via the libbsd library.

这个函数附加字符结束符结尾的字符串src到字符串dest中,复制最多size-strlen(dest)-1到src中,最后附加一个字符结束符,除非size的值小于strlen(dest),这个函数修复了strcat函数的缓冲区溢出的问题,但是调用者必须处理size太小导致数据丢失的可能性,这个函数返回strlcat()试图创建的字符串长度,如果返回值大于或者等于size,就会发生数据丢失,如果数据丢失会有问题,调用者必须在调用前检查参数,或者检测返回值,strlcat既没有在glibc,也没有在POSIX中实现,但是通过libbsd库在linux中有实现

#include <string.h>
#include <stdio.h>

int main(void)
{
	char dest[10];
	const char *src = "abcdefg";
	char *tmp;

	printf("dest = %s\n", dest);
	memset(dest, 0x00, 10);
	tmp = strcat(dest, src);
	printf("tmp = %s\ndest = %s\n", tmp, dest);

	strncat(dest, src, 3);
	printf("dest = %s\n", dest);
	return 0;
}

运行结果如下:

cheny.le@cheny-ThinkPad-T420:~/cheny/testCode$ ./a.out
dest = 0@
tmp = abcdefg
dest = abcdefg
dest = abcdefgabc

这个例子很简单,没什么需要解释的,唯一一个需要注意的地方就是strcat和strncat需要防止dest字符串溢出,本例子dest的空间是10个字节,最后我们打印dest字符串的时候发现能完整的打印出一个占用11个字节的字符串(10个字节的数组abcdefgabc加1个字节的字符串结束符),这个就是发生了溢出,数组结尾后面紧挨着的一个字节就会被改写成0,这个改动是悄无声息的,以至于这是利用缓冲区溢出来攻击安全程序的一种有效途径。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值