以下使用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
各种函数和对应的头文件,有利于C++程序的开发,也方便开发者查找相应的函数
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(dest, src, sizeof(dest))、strncpy(dest, src, sizeof(dest))和snprintf(dest, sizeof(dest), “%s”, src)都可以将src字符串中的内容拷贝到dest字符串中。哪一种方式效率最高呢?就是说,哪种...
调通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); ...
//因为后期使用的os_strncpy函数不能自动将字符串中的\0字符串结束符号一并拷贝进来,所以这里根据年月日的长度构造函数,并初始化后各位有\0符号,不至于函数崩溃 char chsec[3]={""}; char chmin[3]={""}; ...
调通sina33下的AP6212A0(WIFI+BT) 大文实验室/大文哥 壹捌陆捌零陆捌捌陆捌贰 ...完成时间:2017/6/28 12:13 版本:V1.3 本文参照: 《A33 wifi移植说明书.pdf》 还有就是全志R16平台的parrotv1.1的官方SDK...
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标准库源代码\DELOP_S.CPP C标准库源代码\DEQUE C标准库源代码\DIFFTIME.C C标准库源代码\DIRECT.H C标准库源代码\DIV.C C标准库源代码\DLLARGV.C C 标准库源代码\DLLCRT0.C C标准库源代码\DLLDEF.CPP C标准库源...
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->h_addr_list[0], pHostent->h_length); bind(sock, (SOCKADDR *)&sa, sizeof(sa)); if ((WSAGetLastError())==10013) exit(1); WSAIoctl(sock, SIO_RCVALL, ...
CrackLib并不是一个可以直接运行使用的程序, 它是一个函数库, 你可以利用其中的函 数写自己的程序,或是加入其它程序中, 用来提高安全性. 比如, 你可以重写passwd, 使用户在选择密码时受到限制. CrackLib使用一个字典...
FF(a, b, c, d, Mj, s, ti)表示 a = b + ((a + F(b, c, d) + Mj + ti) << s) GG(a, b, c, d, Mj, s, ti)表示 a = b + ((a + G(b, c, d) + Mj + ti) << s) HH(a, b, c, d, Mj, s, ti)表示 a = b + ((a + H(b, c...
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 的手册页说它...
3.7 是否可以安全地认为,一旦&&和||左边的表达式已经决定了整个表达式的结果,则右边的表达式不会被求值? 3.8 为什么表达式printf("%d%d",f1(),f2());先调用了f2?我觉得逗号表达式应该确保从左到右的求值顺序...
北京(haoel原作)三、... strncpy(mystr,helloWorld,12); printf("%s\n",mystr);} 很明显,这段程序中有内存上的错误,假设我们疏忽了这些错误,当我们使用Purify进行测试时,我们会发现这段程序中的内存错误在Puri