@cryptotaxi247 / netdata / commits / 755ec748b

Add build-time check to reject known bad compiler flags. (#20851)

Currently just `-Ofast` and `-ffast-math`, but this will probably be expanded in the future to include other ‘optimization’ flags that allow the compiler to violate guarantees provided by the C/C++ standards.

Austin S. Hemmelgarn committed Aug 20, 2025 at 06:56 UTC 755ec748bed38c68d36c294bcaffc926a0f1cfd3
1 file changed +9
packaging/cmake/Modules/NetdataCompilerFlags.cmake
+9
@@ -89,6 +89,15 @@ function(add_required_compiler_flag flag)
89 endif()
90 endfunction()
91
92 +message(CHECK_START "Checking for known bad compiler flags")
93 +string(REGEX MATCH "(-Ofast|-ffast-math)" BAD_FLAGS "${CMAKE_C_FLAGS}" "${CMAKE_CXX_FLAGS}")
94 +if(BAD_FLAGS)
95 + message(CHECK_FAIL "${BAD_FLAGS}")
96 + message(FATAL_ERROR "Found known bad compiler flag '${BAD_FLAGS}'. This flag allows the compiler to violate the guarantees of the C/C++ standards in ways that are known to break Netdata (and many other things as well). Refusing to build with this compiler flag. Any issues opened about builds that circumvent this check and build with this flag anyway will be closed as invalid.")
97 +else()
98 + message(CHECK_PASS "none found")
99 +endif()
100 +
101 if(CMAKE_BUILD_TYPE STREQUAL "Debug")
102 option(DISABLE_HARDENING "Disable adding extra compiler flags for hardening" TRUE)
103 option(USE_LTO "Attempt to use of LTO when building. Defaults to being enabled if supported for release builds." FALSE)