如何使用Coverity建模将方法标记为不返回?

时间:2017-02-13 05:08:34

标签: java coverity

我正在使用免费Coverity Scan服务进行学习项目,我想将一些方法建模为始终抛出异常,或者调用内部System.exit(),因此不返回,以便从Coverity的流量分析中获得更好的结果。

例如:

class Foo {
    // given these
    Blarg fieldFromTheClass

    void fail(String s, int a, int b) {
        throw new DomainSpecificBlahBlahException(s, someFunction(a), someOtherFunction(a, b), fieldFromTheClass, collaborator.getBaz());
    }

    void fatal(Strin s) {
        System.out.println("Fatal error: " + s);
        System.exit(1);
    }

    // we should get 2 flags here
    void test(int i) {
        if (i%2==0) {
            try {
                fatal("foobar");
            } catch (SecurityException se) { 
                // recovering from security manager - should be flagged as unreachable in normal circumstances
            }
        } else {
            fail("baz", 1, 3);
        }

        doSomethingElse(); // unreachable
    }
}

使用建模文件的方法是什么?

此外,Coverity注释是否可在任何公共存储库(即Bintray或Central)中使用?

1 个答案:

答案 0 :(得分:0)

Coverity应该会自动找出出于某种原因退出或永不返回的功能,但它确实似乎错过了一些。 在这种情况下,您可以对函数进行建模,该模型看起来像这样:

#ifdef __COVERITY__
void fatal(String s) {
    __coverity_panic__();
}
#else
// real definition of fatal

(coverity_installation)/ library中应该有一些例子。