0

我有一个 UndefinedBehaviorSanitizer 构建 ( -fsanitize=undefined),并且我试图在我无法控制的外部库中抑制 UB 的警告。clang/gcc 文档提到__attribute__((no_sanitize("undefined")))了 ,但令我惊讶的是,这个属性似乎并没有抑制来自子例程的警告。

简单的例子

//__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 构建中删除整个目标,但这似乎非常严厉。

4

1 回答 1

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();
}
于 2021-04-13T20:55:36.650 回答