提问人:Jean-Michaël Celerier 提问时间:8/7/2023 更新时间:8/8/2023 访问量:70
如何使用 g++ 检测 ubsan 的存在
How to detect ubsan presence with g++
问:
我有一些有效的 C++ 代码无法在 ubsan 下使用 g++ 编译。 简而言之(https://gcc.godbolt.org/z/9qvz89na8):
struct foo {
void bar() { }
};
void process(auto f) {
if constexpr(&decltype(f)::bar);
}
int main() {
process(foo{});
}
收益 率
In instantiation of 'void process(auto:1) [with auto:1 = foo]':
error: '(foo::bar != 0)' is not a constant expression
因此,我想检测我是否在 ubsan 下构建,以尝试通过制作有条件的 constexpr 来缓解问题。if constexpr
在 clang 下它是微不足道的,但是当我尝试查看预定义的宏时,使用 g++:
echo | g++ -fsanitize=undefined -dM -E -
我看不到任何暗示 ubsan 已启用的内容。有什么方法可以检测到这一点吗?
答:
1赞
Andreas Reischuck
8/8/2023
#1
这是一个似乎有效的预言机。
template<class T>
concept Check = sizeof(int[&T::bar != nullptr]) == sizeof(int[true]);
有关完整示例,请参阅:https://gcc.godbolt.org/z/jra1bbY43。
评论