#include<stdio.h>
#include <stdlib.h>
int test() {
const char* s = getenv("CNU");
if (s!=NULL)
return 1;
else
return -1;
}
int main() {
test();
// some C code..
return 0;
}
我用于覆盖率分析的命令:
cov-build --dir Cov.build gcc test.c
cov-analyze --dir Cov.build --aggressiveness-level high --enable-callgraph-metrics --all
报告:
Analysis summary report:
------------------------
Files analyzed : 1
Total LoC input to cov-analyze : 10926
Functions analyzed : 2
Paths analyzed : 6
Time taken by analysis : 00:00:01
Defect occurrences found : 0
关于CHECKED_RETURN:https://ondemand.coverity.com/reference/7.6.1/en/coverity
CHECKED_RETURN
检查器是一个统计检查器 - 它查找检查返回值的示例,如果达到统计上显着(可配置)的阈值,将为您未检查返回值的位置发出缺陷。
如果您希望它在每次检查返回值时始终发出缺陷,那么您需要添加__coverity_always_check_return__()
,如您链接的文档中的示例所示:
int always_check_me(void) {
__coverity_always_check_return__();
return rand() % 2;
}
int main(int c, char **argv) {
always_check_me(); #defect#checked_return
// the statement above is a defect because the value is not checked
cout << "Hello world" << endl;
}
出于显而易见的原因,您还需要为此源代码创建一个函数存根,以便编译源代码(也在文档中提到)。如果你想只为Coverity生成代码,你可以用#if __COVERITY__
保护它。
是的,CHECKED_RETURN是统计检查错误。如果你在10个地方检查test()的返回值,那么如果你错过了检查test()的返回值,那么在第11位,覆盖率将返回CHECKED_RETURN错误。 Ans显示有关总使用量检出的地点的统计数据。