From 06f2738c03df35d1cad794db71985d57efd72459 Mon Sep 17 00:00:00 2001 From: "Daniel J. Hofmann" Date: Fri, 4 Sep 2015 16:34:34 +0200 Subject: [PATCH] Add stricter compiler warnings to build system. These are for standard compliance and should on by default: -Wall -Wextra -pedantic The problem is that even `-Wall` and `-Wextra` does not cover all warnings, as to not break backward compatibility. Clang therefore has the `-Weverything` flag, that really includes everything but is overkill for the day to day development. Thus, we in addition add: -Wuninitialized -Wunreachable-code to guard against undefined behavior from reading uninitialized variables and warn for unreachable code. With: -Wstrict-overflow=1 the compiler warns us when it's doing optimizations based on the fact that signed integer overflows are undefined behavior. With: -D_FORTIFY_SOURCE=2 we tell the compiler to replace functions like strcpy with strncpy where it can do so, resulting in cheap and useful buffer overflow protection. References: - https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html - https://securityblog.redhat.com/2014/03/26/fortify-and-you/ - https://wiki.debian.org/Hardening --- CMakeLists.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 1f18eda8d..d47a83655 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -155,7 +155,7 @@ endif() if(${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang") # using Clang # -Weverything -Wno-c++98-compat -Wno-shadow -Wno-exit-time-destructors - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wunreachable-code -pedantic -fPIC") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -pedantic -Wuninitialized -Wunreachable-code -Wstrict-overflow=3 -D_FORTIFY_SOURCE=2 -fPIC") elseif(${CMAKE_CXX_COMPILER_ID} STREQUAL "GNU") set(COLOR_FLAG "-fdiagnostics-color=auto") check_cxx_compiler_flag("-fdiagnostics-color=auto" HAS_COLOR_FLAG) @@ -163,7 +163,7 @@ elseif(${CMAKE_CXX_COMPILER_ID} STREQUAL "GNU") set(COLOR_FLAG "") endif() # using GCC - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -pedantic -fPIC ${COLOR_FLAG}") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -pedantic -Wuninitialized -Wunreachable-code -Wstrict-overflow=3 -D_FORTIFY_SOURCE=2 -fPIC ${COLOR_FLAG}") if(WIN32) # using mingw add_definitions(-D_USE_MATH_DEFINES) # define M_PI, M_1_PI etc. add_definitions(-DWIN32)