0

I have an UndefinedBehaviorSanitizer build (-fsanitize=undefined), and I am trying to suppress a warning for UB in an external library that is out of my control. The clang/gcc docs mention __attribute__((no_sanitize("undefined"))), but to my surprise it seems that this attribute does not suppress warnings from subroutines.

Simple example:

//__attribute__((no_sanitize("shift"))) // this correctly suppresses the warning
int bar() {
    return 1 << 64;
}

__attribute__((no_sanitize("shift"))) // this does not
int foo() {
    return bar();
}

int main() {
    foo();
    return 0;
}

Since this attribute doesn't seem to work, how can I suppress this warning? I could remove the entire target from my UBSan build, but that seems incredibly heavy-handed.

0x5453
  • 12,753
  • 1
  • 32
  • 61
  • For extra context, [here](https://gcc.godbolt.org/z/66EhPGsv5) is an example of the specific bug I am trying to suppress. And [here](https://github.com/boostorg/ptr_container/issues/18) is the related bug report in boost. – 0x5453 Apr 13 '21 at 20:47

1 Answers1

1

Clang has pragmas to apply attributes in bulk:

#pragma clang attribute push (__attribute__((no_sanitize("undefined"))), apply_to=function)
// ...
#pragma clang attribute pop

Wrapping the header in those disables the check in your example:

#pragma clang attribute push (__attribute__((no_sanitize("undefined"))), apply_to=function)
#include <boost/ptr_container/ptr_vector.hpp>
#pragma clang attribute pop

struct Foo{};

void bar()
{
    boost::ptr_vector<boost::nullable<Foo>> v;
    v.push_back(nullptr);
    v.push_back(new Foo);
}

int main()
{
    bar();
}
HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207
  • This looks like exactly what I need. Thanks. – 0x5453 Apr 13 '21 at 21:04
  • I’m getting an error `unused attribute 'no_sanitize' in '#pragma clang attribute push' region` when trying exactly that (using Apple’s Clang 13). Any ideas? – Frederik May 24 '22 at 07:49
  • I also got stuck at `unused attribute`; the only workaround I've found was to compile with `-fsanitize-ignorelist=foo.txt` instead, using `src:` lines in `foo.txt` to match troublesome header files. – roystgnr May 23 '23 at 15:49