修复Coverity静态检查工具发现的代码缺陷

coverity介绍

静态源代码分析允许我们在软件开发周期的早期阶段发现和修复缺陷,节省数以百万计的相关成本。

coverity常见软件缺陷修复方法

对于一些不知道修复的缺陷,通过http://cwe.mitre.org/data/index.html网址输入id,查找该缺陷的具体含义,并找到解决方法。

  1. CWE-170: Improper Null Termination
    字符串缺少终止字符,某些函数中访问字符串是以终止字符标记的,缺少的话,会导致越界访问,存在引发异常的风险。
<该例子引用http://cwe.mitre.org/data/definitions/170.html>
#define MAXLEN 1024
...
char *pathbuf[MAXLEN];
...
read(cfgfile,inputbuf,MAXLEN); //does not null terminate
strcpy(pathbuf,inputbuf); //requires null terminated input
...
  1. CWE-563: Assignment to Variable without Use
<该例子引用http://cwe.mitre.org/data/definitions/563.html>
r = getName();
r = getNewBuffer(buf);
  1. CWE-252: Unchecked Return Value
int returnChunkSize(void *) {

/* if chunk info is valid, return the size of usable memory, 

 1. else, return -1 to indicate an error 

*/ 
...
}
int main() {
...
memcpy(destBuf, srcBuf, (returnChunkSize(destBuf)-1));
...
}

If returnChunkSize() happens to encounter an error it will return -1. Notice that the return value is not checked before the memcpy operation (CWE-252), so -1 can be passed as the size argument to memcpy() (CWE-805). Because memcpy() assumes that the value is unsigned, it will be interpreted as MAXINT-1 (CWE-195), and therefore will copy far more memory than is likely available to the destination buffer (CWE-787, CWE-788).

  1. CWE-367: Time-of-check Time-of-use (TOCTOU) Race C
  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值