Skip to content

Commit c4dd260

Browse files
committed
build(cmake): add AddressSanitizer support
1 parent e1a1065 commit c4dd260

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# build directory
22
/cmake-build-debug/
3+
/cmake-build-release/
4+
/cmake-build-sanitize/
35

46
# ide settings
57
/.idea

CMakeLists.txt

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,22 @@ project(objcurses)
55

66
set(CMAKE_CXX_STANDARD 20)
77

8+
# address sanitizer
9+
option(SANITIZE "enable AddressSanitizer with extended checks" OFF)
10+
11+
if(SANITIZE)
12+
message(STATUS "AddressSanitizer enabled")
13+
set(ASAN_FLAGS "-fsanitize=address -fno-omit-frame-pointer -g")
14+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${ASAN_FLAGS}")
15+
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${ASAN_FLAGS}")
16+
17+
# embed runtime ASAN options into binary
18+
add_compile_definitions(ASAN_OPTIONS="detect_leaks=1:strict_string_checks=1:check_initialization_order=1:detect_stack_use_after_return=1:detect_container_overflow=1:abort_on_error=1")
19+
endif()
20+
821
# collect all source files recursively, excluding build directory
922
file(GLOB_RECURSE SOURCES CONFIGURE_DEPENDS "${CMAKE_SOURCE_DIR}/*.cpp")
10-
list(FILTER SOURCES EXCLUDE REGEX "${CMAKE_BINARY_DIR}")
23+
list(FILTER SOURCES EXCLUDE REGEX ".*/.*build.*/.*")
1124

1225
# creating executable
1326
add_executable(${PROJECT_NAME} ${SOURCES})
@@ -19,4 +32,4 @@ target_link_libraries(${PROJECT_NAME} PRIVATE ${CURSES_LIBRARIES})
1932
target_include_directories(${PROJECT_NAME} PRIVATE ${CURSES_INCLUDE_DIR})
2033

2134
# linking math library
22-
target_link_libraries(${PROJECT_NAME} PRIVATE m)
35+
target_link_libraries(${PROJECT_NAME} PRIVATE m)

main.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@
1818
#include "config.h"
1919
#include "version.h"
2020

21+
#ifdef ASAN_OPTIONS
22+
extern "C" const char *__asan_default_options() {
23+
return ASAN_OPTIONS;
24+
}
25+
#endif
26+
27+
2128
// ncurses
2229

2330
void init_ncurses()

0 commit comments

Comments
 (0)