From e13c7ed26111a521efb0eba4c35598c127e46764 Mon Sep 17 00:00:00 2001
From: Sergey Bronnikov <sergeyb@tarantool.org>
Date: Mon, 14 Oct 2024 19:20:12 +0300
Subject: [PATCH] cmake: fix build with GCC's UBSan

GNU GCC compiler has UndefinedBehaviour sanitizer support since
4.9.0 [1], but it was unsupported in tarantool's build. The patch
fixes a build by GNU GCC with enabled UBSan.

1. https://gcc.gnu.org/gcc-4.9/changes.html

NO_CHANGELOG=build
NO_DOC=build
NO_TEST=build

(cherry picked from commit 511e0f50e4b817d576ef4001611fba718ef1bdae)
---
 cmake/compiler.cmake | 18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)

diff --git a/cmake/compiler.cmake b/cmake/compiler.cmake
index 9b8c317e0c..c43ca41d25 100644
--- a/cmake/compiler.cmake
+++ b/cmake/compiler.cmake
@@ -234,8 +234,11 @@ macro(enable_tnt_compile_flags)
     endif()
 
     if (ENABLE_UB_SANITIZER)
-        if (NOT CMAKE_COMPILER_IS_CLANG)
-            message(FATAL_ERROR "Undefined behaviour sanitizer only available for clang")
+        # UndefinedBehaviourSanitizer has been added to GCC since
+        # version 4.9.0, see https://gcc.gnu.org/gcc-4.9/changes.html.
+        if(CMAKE_COMPILER_IS_GNUCC AND
+            CMAKE_C_COMPILER_VERSION VERSION_LESS 4.9.0)
+            message(FATAL_ERROR "UndefinedBehaviourSanitizer is unsupported in GCC ${CMAKE_C_COMPILER_VERSION}")
         endif()
         # Use all needed checks from the UndefinedBehaviorSanitizer
         # documentation:
@@ -255,9 +258,16 @@ macro(enable_tnt_compile_flags)
             # "UBSan: check nonnull-attribute is globally suppressed",
             # https://github.com/tarantool/tarantool/issues/10740
             nonnull-attribute
-            # Not interested in function type mismatch errors.
-            function
         )
+        # GCC has no "function" UB check. See details here:
+        # https://gcc.gnu.org/onlinedocs/gcc/Instrumentation-Options.html#index-fsanitize_003dundefined
+        if(NOT CMAKE_C_COMPILER_ID STREQUAL "GNU")
+            string(JOIN "," UBSAN_IGNORE_OPTIONS
+                ${UBSAN_IGNORE_OPTIONS}
+                # Not interested in function type mismatch errors.
+                function
+            )
+        endif()
         # XXX: To get nicer stack traces in error messages.
         set(SANITIZE_FLAGS "${SANITIZE_FLAGS} -fno-omit-frame-pointer")
         # Enable UndefinedBehaviorSanitizer support.
-- 
GitLab