Remix.run Logo
saghm 3 days ago

Getting failures later after coercing something to a reference is even easier than that; just deference a null pointer when passing to an argument that takes a reference; no warnings or errors! https://godbolt.org/z/xf5d9jKeh

actionfromafar 3 days ago | parent | next [-]

If you are working with C++ in this day and age, regardless of which compiler you use to output your actual binaries, you really owe it to yourself to compile as many source files as possible with other diagnostic tools, foremost clang-tidy.

It will catch that and a lot of iffy stuff.

If you want to go deeper, you can also add your own diagnostics, which can have knowledge specific to your program.

https://clang.llvm.org/extra/clang-tidy/QueryBasedCustomChec...

aidenn0 3 days ago | parent | prev | next [-]

The funny thing is that if you enable ubsan and -O, it optimizes it to unconditionally call __ubsan_handle_type_mismatch_v1; I wonder if it would be tractable to warn when emitting unconditional calls to ubsan traps...

TuxSH 3 days ago | parent | prev [-]

Interestingly, GCC (but not Clang) detects but doesn't warn the UB and emits "ud2" with -O2: https://godbolt.org/z/61aYox7EP