Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make g++ refuse any code that exhibits undefined behaviour?

I would like to add a CXXFLAG to my build systems that force the entire code-base to be well-defined. So every piece of code that exhibits undefined behaviour in a static fashion, should be refused by the compiler.

For instance reinterpret_cast<A*>(someIntPtr)->aMember is without any runtime context undefined (a), while int i = bar(); i /= i; could result in undefined behaviour (b) depending on the runtime evaluation of bar() (which could return zero).
I only expect the (a) cases to be caught, not necessarily the (b) cases.

like image 752
bitmask Avatar asked Dec 07 '22 17:12

bitmask


1 Answers

I'm not sure that your goal is computationally feasible.

However, you'll get moderately close with -Wall -Wextra -Werror; look at the other warning options to see what else you want to enable.

like image 186
Jonathan Leffler Avatar answered Dec 11 '22 12:12

Jonathan Leffler