c++ - 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 的静态分析器的警告:
空指针作为参数传递给“非空”参数
注释掉条件测试s
不nullptr
删除警告。这是预期的行为还是分析仪的问题?
更详细地说:
首先,改变分析器对参数可空性的假设是否是这种条件的预期行为?我可以相信,这是有道理的。我只是对此感到惊讶,我不知道是否确实如此。
其次,看起来有条件将参数的可空性属性(至少就分析器而言)更改为未指定,从而导致警告。但我假设没有指定可空性属性的参数的默认值已经未指定。这是一个错误的假设吗?或者,就分析器而言,条件可能会变得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
}
解决方案
推荐阅读
- android - 在 Android 9 上打开时停止工作,如何解决?
- c# - 您如何以编程方式在自定义 WPF 面板实现中创建新控件?
- java - 我可以使用 TestInfo 接口从 @BeforeEach 中排除单个测试吗?
- html - 如何在加载时停止 FOUT(无样式文本的闪烁)
- android - Kotlin:在折线图 Mpandroidchart 的 x 轴上设置自定义标签
- google-chrome - 哪些政策会阻止加载未打包的 Chrome 扩展程序?
- angular - 尝试实现 Spinner 时出现异常
- python - 多级图像阈值
- php - Laravel - 发送邮件返回错误无法返回非对象的属性
- regex - 在 perl 中解析字符串并执行操作