如何抑制来自 UBsan 的一些无符号整数溢出错误?
How to suppress some unsigned-integer-overflow errors from UBsan?
我的大多数 -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 中不可用。
使用按位 NOT 运算符似乎可以修复所谓的运行时错误。
auto b = ~a;
auto c = ~a - 5;
如果您想将操作包装在一个函数中,您可以像这样使用 __attribute__((no_sanitize("integer")))
(see it live):
__attribute__((no_sanitize("integer")))
unsigned calc( unsigned a )
{
return a - 1 ;
}
我通过错误 report/feature 请求 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.
我的大多数 -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 中不可用。
使用按位 NOT 运算符似乎可以修复所谓的运行时错误。
auto b = ~a;
auto c = ~a - 5;
如果您想将操作包装在一个函数中,您可以像这样使用 __attribute__((no_sanitize("integer")))
(see it live):
__attribute__((no_sanitize("integer")))
unsigned calc( unsigned a )
{
return a - 1 ;
}
我通过错误 report/feature 请求 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.