Skip to content

Commit 9f40a04

Browse files
committed
Add compiler-specific hardening flags for GCC and Clang
1 parent 74f6802 commit 9f40a04

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

CMakeLists.txt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,37 @@ if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
111111
endforeach()
112112
endif()
113113

114+
# Add compiler-specific hardening flags.
115+
if(CMAKE_CXX_COMPILER_ID MATCHES "^(Clang|GNU)$")
116+
add_compile_options(
117+
# Warn about potentially unsafe code.
118+
-Wall
119+
# Warn about implicit conversions that potentially alter a value.
120+
-Wconversion
121+
# Check argument types of format string function calls, e.g., printf.
122+
-Wformat
123+
# Check for potential security issues in format string function calls.
124+
-Wformat-security
125+
# Revert strict aliasing enabled at optimization levels -O2, -O3, -Os.
126+
-fno-strict-aliasing
127+
# Check for buffer overflows such as stack smashing attacks.
128+
-fstack-protector
129+
# Enable fortified wrappers of GNU C library functions.
130+
-D_FORTIFY_SOURCE=2
131+
)
132+
133+
# We need to support CMake 3.10, add_link_options() was added in CMake 3.13.
134+
# Use link_libraries() instead, which passes flags unmodified as long as they
135+
# do not contain spaces, e.g., `-znoexecstack` instead of `-z noexecstack`.
136+
# https://cmake.org/cmake/help/v3.13/command/add_link_options.html
137+
link_libraries(
138+
# Check objects for unresolved symbol references.
139+
-Wl,--no-undefined
140+
# Mark library as not requiring executable stack.
141+
-znoexecstack
142+
)
143+
endif()
144+
114145
# https://clang.llvm.org/docs/AddressSanitizer.html
115146
option(ACL_WITH_ASAN "Build with address sanitizer" OFF)
116147
message(STATUS "Build with address sanitizer: ${ACL_WITH_ASAN}")

0 commit comments

Comments
 (0)