危险函数
来源:互联网 发布:天津程序员招聘 编辑:程序博客网 时间:2024/04/30 03:32
1.strcmy
函数定义:int strcmy(const char* str1, const char* str2)
返回值:若字符串str1> str2, 返回值>0; 若字符串str1==str2, 返回值=0; 若字符串str1 < str2, 返回值 <0
存在的问题:对入参的检测
例如:
<strong><span style="color:#ff0000;">[wln@localhost c]$ g++ -o strcmp strcmp.c [wln@localhost c]$ ./strcmp </span></strong><span style="color:#ff0000;"><strong>段错误 (core dumped)</strong></span>[wln@localhost c]$ cat strcmp.c #include <stdio.h>#include <assert.h>#include <stdlib.h>#include <string.h>/* * 使用strcmy看不验证条件是否存在问题*/int main(void){ const char *str1=NULL; const char *str2="hello"; //判断字符串是否为NULL#if 0 assert (str1 != NULL); assert (str2 != NULL);#endif int result=strcmp(str1,str2); if (result<0) { printf("str1 < str2\n"); } else if(result==0) { printf("str1 == str2\n"); } else { printf("str1 > str2\n"); }}
还有个函数:strncmp函数
int strncmp(const char *s1, const char *s2, size_t n);
个人认为用strncmp 不用strcmp原因是:
(1)本来只需要比较前n位
(2)有可能字符串结束符不同,比如:一个'\n',一个'\r'\n'
...待总结
2. 函数strcpy
入参检测
0 0
- 危险函数
- C语言危险函数
- 如何关闭危险函数
- 危险的_strdup函数
- php禁用一些危险函数
- C语言中的危险函数
- 危险
- 危险
- 危险
- 危险
- 危险
- 危险
- 危险
- 危险
- 危险
- 危险
- 危险
- 危险
- 数据降维--SVD&CUR
- Android仿微信朋友圈图片查看器
- 杭电1172 猜数字(暴力)
- C和C++之间库的调用(转自http://www.cppblog.com/wolf/articles/77828.html)
- HDU 2035 人见人爱
- 危险函数
- IP地址查询接口及调用方法
- SDWebImage缓存图片的机制
- Incomplete Chessboard
- 黑马程序员——常用API(StringBuilder和StringBuffer以及基本数据类型包装类)
- C#编程总结(七)数据加密
- HDOJ 题目1506 Largest Rectangle in a Histogram(动态规划)
- 驱动问题
- HDU-2577 How to Type DP