Issue 76264
Summary diagnose_if runs within false branches of C++ `if constexpr`, leading to false positives
Labels new issue
Assignees
Reporter q66
    minimal reproducer:

```
#include <cstdio>
#include <cstring>
#include <cstddef>

void *copy(void *d, void *s, size_t n) __attribute__((diagnose_if(__builtin_object_size(d, 0) < n, "n larger than d", "error"))) {
    return memcpy(d, s, n);
}

int test(long long v) noexcept
{
    int r = 0;
 if constexpr (sizeof(r) == sizeof(v)) {
        copy(&r, &v, sizeof(v));
    }
    return r;
}

int main() {
    return test(16);
}
```

compiling reveals:

```
$ c++ test.cc -o test    
test.cc:13:31: error: n larger than d
   13 | copy(&r, &v, sizeof(v));
      | ^
test.cc:5:55: note: from 'diagnose_if' attribute on 'copy':
    5 | void *copy(void *d, void *s, size_t n) __attribute__((diagnose_if(__builtin_object_size(d, 0) < n, "n larger than d", "error"))) {
      | ^           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 error generated.
```

The condition `sizeof(r) == sizeof(v)` is always false in this case, yet the diagnostic is still invoked, like if it was true. This does leads to a false positive at compile-time. The workaround is obviously easy (to use `sizeof(r)` instead) but should not be necessary.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to