0

我需要确定特定代码行中变量的可能值范围。在一个复杂的项目中,这可能非常乏味且容易出错。这就是为什么我寻求一种可能性来自动化这项任务。想象一下以下简单的代码片段:

#define checkBoundary(value) if((value)<100 || (value)> 1000) return;

bool checkmaximumForMode0(value)
{
    return (value>100);
}

void main(void)
{
    int mode = rand()%4;
    // x shall be any valid value for an int, just for the sake of completion i use rand here.
    int x = rand();
    if (x < 0) 
        return;
    switch(mode)
    {
    case 0:
        if(checkmaximumForMode0(x)) return;
    case 1: 
        checkBoundary(x);
    default:
        if (x<10000)
            goto exit;
    }
    // Now i want to know, which value range of x will i have under what circumstances
    int isChecked = x;
    // For this easy example:
    // Codepath 1(mode = 0): x >= 0 && x <= 100
    // Codepath 2(mode = 1): x >= 100 && x <= 1000
    // Codepath 3(mode = 2..3): x >= 10000 && x <= maxint
exit:
    print( "Exiting");
    return
}

有没有人知道以完整方式完成此任务的工具或任何其他方式?此外,我想根据一组给定的值范围检查结果值范围,理想情况下只得到不兼容的结果。

4

2 回答 2

1

我们目前正在使用 clang 静态分析器(http://clang-analyzer.llvm.org/)和 cppcheck(也适用于 C):http ://cppcheck.sourceforge.net/

cppcheck(除了许多额外的检查)能够通过分析数据流来检查缓冲区的越界访问。另见:http: //sourceforge.net/p/cppcheck/wiki/ListOfChecks

最后但并非最不重要的一点是,sonarqube 带有一些额外的检查(您必须为 C/C++ 插件付费),并且能够在网页上呈现所有内容。可以连接到 Jenkins 以及集成 cppcheck 结果和/或代码覆盖率: http: //www.sonarqube.org

于 2015-12-02T18:11:51.133 回答
0

Polyspace能够分析值范围,但它是一种专有解决方案。

于 2015-12-04T15:36:06.657 回答