首页 > 解决方案 > Clang静态分析器-具有未指定可空性的参数使“更多”未指定条件?

问题描述

以下:

int dummyStrlen (const char* /*str*/ __attribute__((nonnull))) {
    return 42;
}

void f (const char* s) {
    if (s != nullptr) printf ("notnull"); // Commenting-out this line removes the warning
    if (dummyStrlen (s) > 0) printf ("length OK");
}

https://wandbox.org/permlink/WfIQ19lZ22Mc1bk9

触发 Clang 的静态分析器的警告:

空指针作为参数传递给“非空”参数

注释掉条件测试snullptr删除警告。这是预期的行为还是分析仪的问题?

更详细地说:

首先,改变分析器对参数可空性的假设是否是这种条件的预期行为?我可以相信,这是有道理的。我只是对此感到惊讶,我不知道是否确实如此。

其次,看起来有条件将参数的可空性属性(至少就分析器而言)更改为未指定,从而导致警告。但我假设没有指定可空性属性的参数的默认值已经未指定。这是一个错误的假设吗?或者,就分析器而言,条件可能会变得s 更加不确定(即可疑)?

请注意,如果存在这种用法,则上述内容也受见证用法的影响f

int main() {
    // f(nullptr);                   // Uncommenting this call reintroduces the warning even if the check for nullptr in f is commented-out
    // f("Thanks for all the fish"); // Uncommenting this call suppresses the warning if the above call with nullptr is not also uncommented
}

标签: c++nullableclang-static-analyzer

解决方案


推荐阅读