How to suppress some unsigned-integer-overflow errors from UBsan?

被刻印的时光 ゝ 提交于 2019-12-04 05:22:35

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 ;
}

I found this via a bug report/feature request Suppression support for UbSAN.

The clang documentation on attributes does not indicate any way to apply this except to a function:

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.

Using the bitwise NOT operator seems to fix the alleged runtime error.

auto b = ~a;
auto c = ~a - 5;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!