I have a simple example:
#include <stdio.h>
int main()
{
unsigned long int a;
printf("a = 0x%lx\n", a);
return 0;
}
Clearly, this code has UB. But when I try to sanitise it with:
clang -fsanitize=undefined -O0 -xc main.cpp && ./a.out
it runs "fine". Is this behaviour expected? Should I be using another sanitiser for this?
Update:
- It doesn't seem to work for c++ too with
-fsanitize=memory. - And for dynamic allocations as well.
Update:
I understand that my example is trivial. And a good compiler could detect such issue. I just wrote a simple example to understand if the feature works at all. There is a wide range on uninitialised memory errors that cannot be detected by a compiler. For instance, if an uninitialised memory chunk was allocated in one .cpp and used in another .cpp.