1

我尝试使用带有选项 +fnr 的 PC-Lint。它使 lint 怀疑函数的所有类似指针的返回都可能是 nullptr。

但是对于某些函数,我知道它们在任何情况下都不会返回 nullptr。为了告诉 PC-Lint 它可以忽略它们的 nullptr 检查,我可以使用

-sem(func, @p != 0)

但是还有另一个问题:当标准容器返回迭代器时,PC-Lint 无法正确检查情况。

// using +fnr in .lnt file
//lint -sem(f1, @p != 0)
//lint -sem(std::map::find, @p != 0)
#include<map>

int* f1();

int main()
{
    int* p1 = f1();
    *p1 = 2;            // -sem worked

    std::map<int, int> m;
    std::map<int, int>::iterator it = m.find(1);
    if (it != m.end())
        it->second = 12;// even -sem doesn't work
}

它生成

C:\lint_test\f3.cpp(18): Issue 613: (Warning -- Possible use of null pointer 'unknown-name' in left argument to operator '->' [Reference: file C:\lint_test\f3.cpp: line 18])

如果 PC-Lint 不够聪明,看不到我在使用之前检查了迭代器,我想至少关闭对 std::map::find 的检查。但我不能,-sem 不起作用

有谁知道如何解决这个问题?

4

0 回答 0