0

I need to determine the possible value range of a variable in a specific line of code. In a complex project this can be very tedious and error prone. Thats why i seek a possibility to automate this task. Imagine the following easy code snippet:

#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
}

Does anybody know of a tool or any other way for achieving this task in a complete manner? Furthermore i want to check the resulting value ranges against a given set of value ranges and ideally get only incompatibilities as a result.

2 Answers2

1

We're currently using the clang static analyzer (http://clang-analyzer.llvm.org/) and cppcheck (which works for C as well): http://cppcheck.sourceforge.net/

cppcheck is (besides lot of additional checks) able to check out of bounds access on buffers by analyzing the data flow. See also: http://sourceforge.net/p/cppcheck/wiki/ListOfChecks.

Last but not least sonarqube comes with some additional checks (you have to pay for the C/C++ plugin) and is able to present everything on a web page. Connection to Jenkins is possible as well as integration of cppcheck results and/or code coverage: http://www.sonarqube.org

Joerg S
  • 4,730
  • 3
  • 24
  • 43
0

Polyspace is capable of analyzing value ranges, but its a proprietary solution.

orbitcowboy
  • 1,438
  • 13
  • 25