strcpy ,strncpy ,strlcpy和snprintf的使用
来源:互联网 发布:java程序中使用@的注释 编辑:程序博客网 时间:2024/04/30 15:47
strcpy ,strncpy ,strlcpy的用法
好多人已经知道利用strncpy替代strcpy来防止缓冲区越界。
但是如果还要考虑运行效率的话,也许strlcpy是一个更好的方式。
1. strcpy
strcpy 是依据 /0 作为结束判断的,如果 to 的空间不够,则会引起 buffer overflow。strcpy 常规的实现代码如下(来自 OpenBSD 3.9):
char * strcpy(char *to, const char *from)
{
char *save = to;
for (; (*to = *from) != '/0'; ++from, ++to);
return(save);
}
但通常,我们的 from 都来源于用户的输入,很可能是非常大的一个字符串,因此 strcpy 不够安全。
2. strncpy
在 ANSI C 中,strcpy 的安全版本是 strncpy
char *strncpy(char *s1, const char *s2, size_t n);
但 strncpy 其行为是很诡异的(不符合我们的通常习惯)。标准规定 n 并不是 sizeof(s1),而是要复制的 char 的个数。一个最常见的问题,就是 strncpy 并不帮你保证 /0
结束。
char buf[8];
strncpy( buf, "abcdefgh", 8 );
看这个程序,buf 将会被 "abcdefgh" 填满,但却没有 /0 结束符了。
另外,如果 s2 的内容比较少,而 n 又比较大的话,strncpy 将会把之间的空间都用 /0 填充。这又出现了一个效率上的问题,如下:
char buf[80];
strncpy( buf, "abcdefgh", 79 );
上面的 strncpy 会填写 79 个 char,而不仅仅是 "abcdefgh" 本身。
strncpy 的标准用法为:(手工写上 /0)
strncpy(path, src, sizeof(path) - 1);
path[sizeof(path) - 1] = '/0';
len = strlen(path);
3. strlcpy
// 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);
而使用 strlcpy,就不需要我们去手动负责 /0 了,仅需要把 sizeof(dst) 告之 strlcpy 即可:
strlcpy(path, src, sizeof(path));
len = strlen(path);
if ( len >= sizeof(path) )
printf("src is truncated.");
并且 strlcpy 传回的是 strlen(str),因此我们也很方便的可以判断数据是否被截断。
[* 一点点历史 *]
strlcpy 并不属于 ANSI C,至今也还不是标准。
strlcpy 来源于 OpenBSD 2.4,之后很多 unix-like 系统的 libc 中都加入了 strlcpy 函数,我个人在 FreeBSD、Linux 里面都找到了 strlcpy。(Linux使用的是 glibc,
glibc里面有 strlcpy,则所有的 Linux 版本也都应该有 strlcpy)
但 Windows 下是没有 strlcpy 的,对应的是strncpy和memset函数
- 外文名
- int snprintf(char *str, size_t size, const char *format,
- 原 型
- snprintf()
- 所需头文件
- #include <stdio.h>
- 应 用
- pc
目录
- 1功能
- 2所需头文件
- 3函数返回值
- 4说明
- 5例子
功能
编辑#include <stdio.h>
int
main () {
char
a[16];
size_t
i;
i = snprintf(a, 13,
"%012d"
, 12345);
// 第 1 种情况
printf
(
"i = %lu, a = %s\n"
, i, a);
// 输出:i = 12, a = 000000012345
i = snprintf(a, 9,
"%012d"
, 12345);
// 第 2 种情况
printf
(
"i = %lu, a = %s\n"
, i, a);
// 输出:i = 12, a = 00000001
return
0;
}
所需头文件
编辑函数返回值
编辑说明
编辑snprintf(s,100,
"%.*S"
,3,
"abcd"
);
#if_MSC_VER
#define snprintf _snprintf
#endif
_sntprintf
例子
编辑#include<stdio.h>
#include<stdlib.h>
int
main()
{
char
str[10]={0};
int
nLen=snprintf(str,
sizeof
(str),
"0123456789012345678"
);
printf
(
"str=%s\n"
,str);
printf
(
"nLen=%d\n"
,nLen);
return0;
}
- strcpy ,strncpy ,strlcpy和snprintf的使用
- strcpy ,strncpy ,strlcpy, snprintf, memcpy 用法、区别和效率
- strcpy strncpy和strlcpy
- strcpy、 strncpy 和snprintf
- strcpy strncpy 和strlcpy 【转】
- strcpy, strncpy, strlcpy的区别
- strcpy ,strncpy ,strlcpy的用法
- strcpy ,strncpy ,strlcpy的用法
- strcpy ,strncpy ,strlcpy的用法
- Snprintf Strncpy Memcpy Strlcpy各自的特点
- strcpy、strncpy、strncpy_s和snprintf
- error C4996 & strcpy ,strncpy ,strlcpy的用法
- strcpy,strncpy,strlcpy,memcpy
- strcpy, strncpy and strlcpy
- strcpy,strncpy,strlcpy,memcpy
- strcpy,strncpy,strlcpy,memcpy
- strcpy strncpy strlcpy
- strcpy ,strncpy ,strlcpy
- HDU 5510 Bazinga 【strstr】
- java垃圾回收机制
- c++基础知识
- ubuntu16.04 安装 qq
- [UESTC 1061]秋实大哥与战争
- strcpy ,strncpy ,strlcpy和snprintf的使用
- 【字符串匹配】——KMP(看毛片算法)——深入讲解next数组的求解
- Java Web ---- 文件上传下载demo
- Hiho 118
- 使用Intent传递对象的两种方式
- OFD[4]-第三章 远期和期货价格-2
- 第六周 项目6-停车场模拟
- java7 MethodHandle学习笔记
- Java之——实现每天定时执行任务