lördag 27 juli 2019

Static analysis -- safe interfaces

Static analysis -- safe interfaces

My plan is to add more checks for "safe interfaces" in Cppcheck. Some analysis has already been added but more will be added soon.

Whole program analysis - Detect existing problems

Static analysis tools normally use whole program analysis to determine possible argument values for functions.

Example code:

    int average(int value1, int value2)
    {
        return (value1 + value2) / 2;
    }

    int main()
    {
        average(100,200);
        return 0;
    }

If you analyze this code with a static analysis tool you will not get any warnings. There is no bug.

Safe analysis

Cppcheck will be able to check that functions are robust.

There can be a signed integer overflow in the average function. For instance if it would be called like this:

    average(INT_MAX, 1);


Latest Cppcheck will tell you about the signed integer overflow, if you turn on the "safe checks".

Configuration

I believe that for must functions, you will only want to get warnings based on whole program analysis.

The "safe analysis" will not be used unless it is turned on.

I envision that "safe analysis" might be turned on in such cases:
  • If you design a library and want that the library interface is robust. You want to check all possible calls.
  • If you design a utility function/class and want that it is robust for all possible usage.
  • During code review, you could temporarily turn this on to see potential UB.
  • If you're going to release a critical software, I believe it would be good to turn on such analysis on the "alpha" release and check all the reported warnings.
We need to have detailed configuration options. It should be possible to turn it on permanently for specific functions or classes. To help "safe analysis" more annotation options will be needed also. I have not figured out fully how to configure it.. but to start with the primary way to configure it will be through the cppcheck gui (in the project settings dialog).

The most dangerous software weaknesses in 2020

Mitre has released a list of the most dangerous software weaknesses. https://cwe.mitre.org/top25/archive/2020/2020_cwe_top25.html Item #2 on...