strcat

strcat

From Wikipedia, the free encyclopedia

Jump to:  navigationsearch

In computing, the C programming language offers a library function called strcat that allows one memory block to be appended to another memory block. Both memory blocks are required to be null-terminated. Since, in C, strings are not first-class datatypes, and are implemented as blocks ofASCII bytes in memory, strcat will effectively append one string to another given two pointers to blocks of allocated memory. The name strcat is an abbreviation of "string concatenate". strcatis found in the string.h header file.

For example:

char str1[100] = "Hello,"; strcat(str1, " world!"); puts(str1);

Here is a possible implementation of strcat:

char * strcat(char *dest, const char *src) { size_t dest_len = strlen(dest); size_t i;for (i = 0 ; src[i] != '\0' ; i++) dest[dest_len + i] = src[i]; dest[dest_len + i] ='\0'return dest; }

It can also be defined in terms of other string library functions:

char * strcat(char *dest, const char *src) { strcpy(dest + strlen(dest), src)returndest; }

[edit] Bounds errors

strcat can be dangerous because if the string to be appended is too long to fit in the destination buffer, it will overwrite adjacent memory, invoking undefined behavior. Usually the program will simply cause a segmentation fault when this occurs, but a skilled attacker can use such a buffer overflow to hack into a system (see computer security).

The bounded variant strncat does the same thing as strcat but as it only appends a specified number of bytes, it is susceptible to two types of buffer overflows. The first can only happen when the specified number of bytes is too large to fit in the destination string. The second is when the destination string can only hold the exact number of bytes specified to copy. This will result in an off by one error, and is often exploitable by a skilled attacker. OpenBSD strlcat is regarded by the OpenBSD developers as a safer version of these variants.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值