From d9b4b75236630b9e9918351ae5aa10f697889c7c Mon Sep 17 00:00:00 2001 From: superp00t Date: Sun, 21 Jul 2024 17:12:14 -0400 Subject: [PATCH] feat(build): enable UBsan by default when using GCC and Clang --- CMakeLists.txt | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index db33305..ae295ed 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -28,9 +28,20 @@ set(CMAKE_BUILD_TYPE Debug) include(lib/system/cmake/system.cmake) -# Some templates abuse offsetof -if(WHOA_SYSTEM_LINUX OR WHOA_SYSTEM_MAC) +# Compiler options +if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang" or CMAKE_CXX_COMPILER_ID STREQUAL "GNU") + # Some templates abuse offsetof set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-invalid-offsetof") + + if(CMAKE_BUILD_TYPE STREQUAL "Debug" AND NOT DEFINED WHOA_ALLOW_UNDEFINED_BEHAVIOR) + # Enable UBsan + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=undefined") + # Allow strange alignments + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-sanitize=alignment") + else() + # Disable UBsan completely + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-sanitize=undefined") + endif() endif() # OS defines