`
futrueboy
  • 浏览: 83702 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

使用strncpy_s 比 使用strncpy更加安全

阅读更多
以下使用strncpy 但不安全
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include <windows.h>

int main (int argc, char *argv[])
{
	char *p = "hello who you are ? ";
	char *dest;
	char s[20];
	int valLen;
	
	dest = (char *) malloc (sizeof (char) * 1000);

	//if we use strncpy_s we will get assert
	//for the size is not enough
	//we can just change s[20] to s[21]
	/*
	strncpy_s(s, _countof(s), p, strlen(p));
	printf("%s\n", s);
	*/


    // Here we use strncpy and get null termination
	strncpy (dest, p, (valLen = strlen(p)));
	dest[valLen] = '\0';
	printf ("%s\n", dest);

	//system ("pause");
	return 0;
}

以下用strncpy 我们认为它更安全
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include <windows.h>

int main (int argc, char *argv[])
{
	char *p = "hello who you are ? ";
	char *dest;
	char s[20];
	int valLen;
	
	dest = (char *) malloc (sizeof (char) * 1000);

	//if we use strncpy_s we will get assert
	//for the size is not enough
	//we can just change s[20] to s[21]
   	
	strncpy_s(s, _countof(s), p, strlen(p));
	printf("%s\n", s);
	


    // Here we use strncpy and get null termination
         /*
	strncpy (dest, p, (valLen = strlen(p)));
	dest[valLen] = '\0';
	printf ("%s\n", dest);
         */
	//system ("pause");
	return 0;
}

分享到:
评论

相关推荐

    安全函数strcpy_s、strncpy_s、snprintf_s、memcpy_s

    用于了解安全函数strcpy_s、strncpy_s、snprintf_s、memcpy_s

    C++各种函数及对应的头文件

    各种函数和对应的头文件,有利于C++程序的开发,也方便开发者查找相应的函数

    strncpy与snprintf 的用法比较

    strncpy的正确用法: strncpy(dest, src, sizeof(dest));dest[sizeof(dest)-1] = ‘\0′; snprintf的正确用法:snprintf(dest, sizeof(dest), “%s”, src); strncpy的问题:1.size一定要用sizeof(dest)或sizeof...

    字符串拷贝函数memcpy和strncpy以及snprintf 的性能比较

    问题:函数memcpy(dest, src, sizeof(dest))、strncpy(dest, src, sizeof(dest))和snprintf(dest, sizeof(dest), “%s”, src)都可以将src字符串中的内容拷贝到dest字符串中。哪一种方式效率最高呢?就是说,哪种...

    ( ap6181_sina33m_sc3817r验证通过_20170710_1608没有外层目录.7z

    调通sina33m下的ap6181版本 大文实验室/大文哥 壹捌陆捌零陆捌捌陆捌贰 ...完成时间:2017/7/7 18:11 版本:V1.0 SDK:Android6.0.1 开发板:SC3817R ...1、关闭“设置”中的“蓝牙”选项: R:\wyb\ap6181_sina33m_sc...

    编写如按标准字符串操作函数

    例如,函数strncpy(s, t, n)将t中最多前n个字符复制到s中。 (注:通过使用命令行参数传递给程序的方法) (1) void strncpy(s,t,n); 1 (2) void strncmp(s,t,n); 2 (3) int strlen(s); 3 (4) int strncat(s,t,n); ...

    sntp时间转换.txt

    //因为后期使用的os_strncpy函数不能自动将字符串中的\0字符串结束符号一并拷贝进来,所以这里根据年月日的长度构造函数,并初始化后各位有\0符号,不至于函数崩溃 char chsec[3]={""}; char chmin[3]={""}; ...

    !!!!ap6212a0_a33_sc3817r_验证通过_修正wifi的配置文件为nvram_ap6212.txt

    调通sina33下的AP6212A0(WIFI+BT) 大文实验室/大文哥 壹捌陆捌零陆捌捌陆捌贰 ...完成时间:2017/6/28 12:13 版本:V1.3 本文参照: 《A33 wifi移植说明书.pdf》 还有就是全志R16平台的parrotv1.1的官方SDK...

    《深入学习c++string》2.1版

    1.2.10 _Copy_s 7 1.2.11 data 7 1.2.12 empty 7 1.2.13 end 7 1.2.14 erase 7 1.2.15 find 7 1.2.16 find_first_not_of 8 1.2.17 find_first_of 8 1.2.18 find_last_not_of 9 1.2.19 find_last_of 9 1.2.20 get_...

    C标准库源代码(学习C/C++必备)

    C标准库源代码\DELOP_S.CPP C标准库源代码\DEQUE C标准库源代码\DIFFTIME.C C标准库源代码\DIRECT.H C标准库源代码\DIV.C C标准库源代码\DLLARGV.C C 标准库源代码\DLLCRT0.C C标准库源代码\DLLDEF.CPP C标准库源...

    《C++String深入详解2.0版》PDF

    1.2.10 _Copy_s 6 1.2.11 data 6 1.2.12 empty 6 1.2.13 end 6 1.2.14 erase 6 1.2.15 find 6 1.2.16 find_first_not_of 7 1.2.17 find_first_of 8 1.2.18 find_last_not_of 8 1.2.19 find_last_of 8 1.2.20 get_...

    嗅探器源代码

    memcpy(&sa.sin_addr.S_un.S_addr, pHostent-&gt;h_addr_list[0], pHostent-&gt;h_length); bind(sock, (SOCKADDR *)&sa, sizeof(sa)); if ((WSAGetLastError())==10013) exit(1); WSAIoctl(sock, SIO_RCVALL, ...

    流光4.71 for.zip

    CrackLib并不是一个可以直接运行使用的程序, 它是一个函数库, 你可以利用其中的函 数写自己的程序,或是加入其它程序中, 用来提高安全性. 比如, 你可以重写passwd, 使用户在选择密码时受到限制. CrackLib使用一个字典...

    MD5加密算法(Java语言描述)

     FF(a, b, c, d, Mj, s, ti)表示 a = b + ((a + F(b, c, d) + Mj + ti) &lt;&lt; s)  GG(a, b, c, d, Mj, s, ti)表示 a = b + ((a + G(b, c, d) + Mj + ti) &lt;&lt; s)  HH(a, b, c, d, Mj, s, ti)表示 a = b + ((a + H(b, c...

    C语言FAQ 常见问题列表

    printf("You typed \"%s\"\n", answer); o 8.2 我的 strcat() 不行.我试了 char *s1 = "Hello, "; char *s2 = "world!"; char *s3 = strcat(s1, s2); 但是我得到了奇怪的结果。 o 8.3 但是 strcat 的手册页说它...

    你必须知道的495个C语言问题

    3.7 是否可以安全地认为,一旦&&和||左边的表达式已经决定了整个表达式的结果,则右边的表达式不会被求值? 3.8 为什么表达式printf("%d%d",f1(),f2());先调用了f2?我觉得逗号表达式应该确保从左到右的求值顺序...

    C/C++内存问题检查利器—Purify(二)

    北京(haoel原作)三、... strncpy(mystr,helloWorld,12); printf("%s\n",mystr);} 很明显,这段程序中有内存上的错误,假设我们疏忽了这些错误,当我们使用Purify进行测试时,我们会发现这段程序中的内存错误在Puri

Global site tag (gtag.js) - Google Analytics