From 804e6190d75f7f0f8eb251c21d05a55ff0f35c12 Mon Sep 17 00:00:00 2001 From: Reinhard Urban Date: Fri, 6 Jan 2023 10:51:34 +0100 Subject: AX_ADD_FORTIFY_SOURCE: add -D_FORTIFY_SOURCE=3 check --- m4/ax_add_fortify_source.m4 | 47 ++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 44 insertions(+), 3 deletions(-) (limited to 'm4') diff --git a/m4/ax_add_fortify_source.m4 b/m4/ax_add_fortify_source.m4 index 7e15312..1266048 100644 --- a/m4/ax_add_fortify_source.m4 +++ b/m4/ax_add_fortify_source.m4 @@ -10,7 +10,7 @@ # # Check whether -D_FORTIFY_SOURCE=2 can be added to CPPFLAGS without macro # redefinition warnings, other cpp warnings or linker. Some distributions -# (such as Gentoo Linux) enable _FORTIFY_SOURCE globally in their +# (such as Ubuntu or Gentoo Linux) enable _FORTIFY_SOURCE globally in their # compilers, leading to unnecessary warnings in the form of # # :0:0: error: "_FORTIFY_SOURCE" redefined [-Werror] @@ -29,7 +29,7 @@ # LICENSE # # Copyright (c) 2017 David Seifert -# Copyright (c) 2019 Reini Urban +# Copyright (c) 2019, 2023 Reini Urban # # Copying and distribution of this file, with or without modification, are # permitted in any medium without royalty provided the copyright notice @@ -42,6 +42,46 @@ AC_DEFUN([AX_ADD_FORTIFY_SOURCE],[ ac_save_cflags=$CFLAGS ac_cwerror_flag=yes AX_CHECK_COMPILE_FLAG([-Werror],[CFLAGS="$CFLAGS -Werror"]) + ax_add_fortify_3_failed= + AC_MSG_CHECKING([whether to add -D_FORTIFY_SOURCE=3 to CPPFLAGS]) + AC_LINK_IFELSE([ + AC_LANG_PROGRAM([], + [[ + #ifndef _FORTIFY_SOURCE + return 0; + #else + _FORTIFY_SOURCE_already_defined; + #endif + ]] + )], + AC_LINK_IFELSE([ + AC_LANG_SOURCE([[ + #define _FORTIFY_SOURCE 3 + #include + int main() { + char *s = " "; + strcpy(s, "x"); + return strlen(s)-1; + } + ]] + )], + [ + AC_MSG_RESULT([yes]) + CFLAGS=$ac_save_cflags + CPPFLAGS="$CPPFLAGS -D_FORTIFY_SOURCE=3" + ], [ + AC_MSG_RESULT([no]) + CFLAGS=$ac_save_cflags + ax_add_fortify_3_failed=1 + ], + ), + [ + AC_MSG_RESULT([no]) + CFLAGS=$ac_save_cflags + ax_add_fortify_3_failed=1 + ]) + if test -n "$ax_add_fortify_3_failed" + then AC_MSG_CHECKING([whether to add -D_FORTIFY_SOURCE=2 to CPPFLAGS]) AC_LINK_IFELSE([ AC_LANG_PROGRAM([], @@ -49,7 +89,7 @@ AC_DEFUN([AX_ADD_FORTIFY_SOURCE],[ #ifndef _FORTIFY_SOURCE return 0; #else - this_is_an_error; + _FORTIFY_SOURCE_already_defined; #endif ]] )], @@ -77,4 +117,5 @@ AC_DEFUN([AX_ADD_FORTIFY_SOURCE],[ AC_MSG_RESULT([no]) CFLAGS=$ac_save_cflags ]) + fi ]) -- cgit v1.2.1