gpt4 book ai didi

c++ - 如何抑制来自 UBsan 的一些无符号整数溢出错误?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:18:26 24 4
gpt4 key购买 nike

我的大部分 -fsanitize=unsigned-integer-overflow 错误都是错误,但有时我会按预期明确使用它,这会导致 UBSan 产生误报。

有没有办法为特定表达式关闭 UBSan 无符号整数溢出检查?

编辑以回应 Shafik 评论,这里是一个例子:

unsigned a = 0;
unsigned b = a - 1; // error: unsigned integer overflow

大多数时候这是一个错误,有时不是。有了 UBSan,人们可以发现每次发生的错误,修复错误,但我还没有找到消除误报的方法。

编辑 2:要启用检查,需要通过 -fsanitize=integer(启用所有整数检查)或 fsanitize=unsigned-integer-overflow。从下面的评论来看,该检查似乎仅在 clang 中可用,而在 GCC 中不可用。

最佳答案

如果您想将操作包装在一个函数中,您可以像这样使用 __attribute__((no_sanitize("integer"))) ( see it live ):

__attribute__((no_sanitize("integer")))
unsigned calc( unsigned a )
{
return a - 1 ;
}

我通过错误报告/功能请求找到了这个 Suppression support for UbSAN .

clang documentation on attributes不表示除函数外的任何应用方式:

Use the no_sanitize attribute on a function declaration to specify that a particular instrumentation or set of instrumentations should not be applied to that function. The attribute takes a list of string literals, which have the same meaning as values accepted by the -fno-sanitize= flag. For example, attribute((no_sanitize("address", "thread"))) specifies that AddressSanitizer and ThreadSanitizer should not be applied to the function.

关于c++ - 如何抑制来自 UBsan 的一些无符号整数溢出错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33351891/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com