Tip #4 1/2: Check out compiler safety options
Did tip 4 worry you? It shouldn’t, because it was really a tool to improve your code. But here’s a tip that’s the other way around: compiler settings to use to make your code safer.
- Did you know that some null pointer checks can be optimised out, if the compiler thinks you’ve already dereferenced it?
-fno-delete-null-pointer-checks - Did you know that there’s a warning to detect when a switch case falls through?
-Wimplicit-fallthrough
The classic compiler never told you about that! - Did you know there are multiple switches to check format statements (eg printf) usage?
You can find all these on these two fantastic resources:
- Compiler Options Hardening Guide for C and C++
by the Open Source Security Foundation (OpenSSF) Best Practices Working Group - Leveraging Your Toolchain to Improve Security
by Phillip Johnston
Both these cover multiple compilers and standard libraries. Check for the flags that apply to Clang, version <= 15, and to libc++ version <= 15, which is LLVM’s STL and the one we use. (Flags for versions 16-18 are not yet applicable but will be in future.) There are some great flags there that will make your code better — and that you can use with the new C++Builder 12.2 and it’s Windows 64-bit Modern C++ toolchain.