禁止来自外部库的 UndefinedBehaviorSanitizer 警告

Suppress UndefinedBehaviorSanitizer warnings from external libraries

提问人:0x5453 提问时间:4/14/2021 更新时间:4/14/2021 访问量:1132

问:

我有一个 UndefinedBehaviorSanitizer 构建 (),并且我正在尝试在我无法控制的外部库中抑制 UB 的警告。clang/gcc 文档提到了 __attribute__((no_sanitize(“undefined”))),但令我惊讶的是,这个属性似乎并没有抑制来自子例程的警告。-fsanitize=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 构建中删除整个目标,但这似乎非常严厉。

C++ 禁止警告 ubsan

评论

0赞 0x5453 4/14/2021
为了获得额外的上下文,这里是我试图抑制的特定错误的示例。这是 boost 中的相关错误报告。

答:

1赞 HolyBlackCat 4/14/2021 #1

Clang 具有批量应用属性的编译指示:

#pragma clang attribute push (__attribute__((no_sanitize("undefined"))), apply_to=function)
// ...
#pragma clang attribute pop

将标头包装在 those 中会禁用示例中的检查:

#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();
}

评论

0赞 0x5453 4/14/2021
这看起来正是我需要的。谢谢。
0赞 Frederik 5/24/2022
我在尝试这样做时遇到错误(使用 Apple 的 Clang 13)。有什么想法吗?unused attribute 'no_sanitize' in '#pragma clang attribute push' region
0赞 roystgnr 5/23/2023
我也被困在了;我发现的唯一解决方法是进行编译,使用行来匹配麻烦的头文件。unused attribute-fsanitize-ignorelist=foo.txtsrc:foo.txt