问题描述
我的大部分 -fsanitize=unsigned-integer-overflow
错误都是错误,但有时我会按预期明确使用它,这会导致 UBSan 产生误报.
Most of my -fsanitize=unsigned-integer-overflow
errors are bugs, but sometimes I explicitly use it as intended, which results in UBSan producing false positives.
有没有办法关闭特定表达式的 UBSan 无符号整数溢出检查?
Is there a way to turn UBSan unsigned-integer-overflow check off for a particular expression?
编辑以回应 Shafik 的评论,这是一个例子:
EDIT in response to Shafik comment, here is an example:
unsigned a = 0;
unsigned b = a - 1; // error: unsigned integer overflow
大多数时候这是一个错误,有时不是.使用 UBSan 可以发现每次发生的情况,修复错误,但我还没有找到一种方法来消除误报.
Most of the time that is a bug, sometimes it isn't. With UBSan one can find every time that happens, fix the bugs, but I haven't found a way to silence the false positives.
EDIT 2:要启用检查,需要通过 -fsanitize=integer
(启用所有整数检查)或 fsanitize=unsigned-integer-overflow
.从下面的评论来看,该检查似乎仅在 clang 中可用,而在 GCC 中尚不可用.
EDIT 2: to enable the check one needs to pass either -fsanitize=integer
(to enable all integer checks) or fsanitize=unsigned-integer-overflow
. From the comments below it seems that the check is only available in clang and not in GCC yet.
推荐答案
如果你想将操作包装在一个函数中,你可以像这样使用 __attribute__((no_sanitize("integer")))
(现场观看):
If you want to wrap the operation in a function you can use __attribute__((no_sanitize("integer")))
like so (see it live):
__attribute__((no_sanitize("integer")))
unsigned calc( unsigned a )
{
return a - 1 ;
}
我是通过错误报告/功能请求发现的对 UbSAN 的抑制支持.
I found this via a bug report/feature request Suppression support for UbSAN.
clang 关于属性的文档 并没有指出任何应用它的方法,除了应用于功能:
The clang documentation on attributes does not indicate any way to apply this except to a function:
在函数声明中使用 no_sanitize 属性来指定不应将特定检测或检测集应用于该函数.该属性采用字符串文字列表,其含义与 -fno-sanitize= 标志接受的值相同.例如,attribute((no_sanitize("address", "thread"))) 指定不应将 AddressSanitizer 和 ThreadSanitizer 应用于函数.
这篇关于如何抑制来自 UBsan 的一些无符号整数溢出错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!