【问题标题】:Suppress UndefinedBehaviorSanitizer warnings from external libraries抑制来自外部库的 UndefinedBehaviorSanitizer 警告
【发布时间】:2021-07-08 22:30:14
【问题描述】:
我有一个 UndefinedBehaviorSanitizer 构建 (-fsanitize=undefined),我试图在我无法控制的外部库中抑制 UB 警告。 clang/gcc 文档提到了__attribute__((no_sanitize("undefined"))),但令我惊讶的是,这个属性似乎并没有抑制来自子程序的警告。
Simple example:
//__attribute__((no_sanitize("shift"))) // this correctly suppresses the warning
int bar() {
return 1 << 64;
}
__attribute__((no_sanitize("shift"))) // this does not
int foo() {
return bar();
}
int main() {
foo();
return 0;
}
由于此属性似乎不起作用,我该如何抑制此警告?我可以从我的 UBSan 构建中删除整个目标,但这似乎非常严厉。
【问题讨论】:
-
对于额外的上下文,here 是我试图抑制的特定错误的一个示例。 here 是 boost 中相关的错误报告。
标签:
c++
suppress-warnings
ubsan
【解决方案1】:
Clang 具有批量应用属性的编译指示:
#pragma clang attribute push (__attribute__((no_sanitize("undefined"))), apply_to=function)
// ...
#pragma clang attribute pop
将标头包装在其中会禁用示例中的检查:
#pragma clang attribute push (__attribute__((no_sanitize("undefined"))), apply_to=function)
#include <boost/ptr_container/ptr_vector.hpp>
#pragma clang attribute pop
struct Foo{};
void bar()
{
boost::ptr_vector<boost::nullable<Foo>> v;
v.push_back(nullptr);
v.push_back(new Foo);
}
int main()
{
bar();
}