微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

抑制来自外部库的 UndefinedBehaviorSanitizer 警告

如何解决抑制来自外部库的 UndefinedBehaviorSanitizer 警告

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

解决方法

Clang 有 pragma 来批量应用属性:

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。