Commit d9c4c7fe282bbf79fdf3af6aa4b870125e822a50

Guillem Jover 2021-02-20T22:19:49

build: Add support for sanitizer compiler flags

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 6ff42e2..3697f19 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -8,7 +8,7 @@ before_script:
 unit-tests:
   stage: test
   script:
-    - ./autogen && ./configure
+    - ./autogen && ./configure --enable-sanitize
     - make check
 
 coverage:
diff --git a/configure.ac b/configure.ac
index 11db6c7..1530ecf 100644
--- a/configure.ac
+++ b/configure.ac
@@ -70,6 +70,19 @@ AS_IF([test "$user_CFLAGS" = unset], [
   LIBMD_CHECK_COMPILER_FLAG([-Wwrite-strings])
 
   CFLAGS="$LIBMD_COMPILER_FLAGS $CFLAGS"
+
+  AC_ARG_ENABLE([sanitize],
+    [AS_HELP_STRING([--enable-sanitize], [enable compiler sanitizer support])],
+  [
+    LIBMD_COMPILER_FLAGS=''
+
+    LIBMD_CHECK_COMPILER_FLAG([-fsanitize=address])
+    LIBMD_CHECK_COMPILER_FLAG([-fsanitize=leak])
+    LIBMD_CHECK_COMPILER_FLAG([-fsanitize=undefined])
+
+    CFLAGS="$LIBMD_COMPILER_FLAGS $CFLAGS"
+    LDFLAGS="$LIBMD_COMPILER_FLAGS $LDFLAGS"
+  ])
 ])
 
 # Checks for libraries.