危险函数

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值