Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

error: "_FORTIFY_SOURCE" redefined [-Werror] #285

Open
stintel opened this issue Mar 10, 2025 · 2 comments
Open

error: "_FORTIFY_SOURCE" redefined [-Werror] #285

stintel opened this issue Mar 10, 2025 · 2 comments

Comments

@stintel
Copy link

stintel commented Mar 10, 2025

Host build of ucode fails on my system:

[3/40] /home/stijn/Development/OpenWrt/openwrt/staging_dir/host/bin/gcc -DHAVE_ARRAY_EXT -DHAVE_JSON_UINT64 -DHAVE_PARSE_END -DLIB_SEARCH_PATH="\"/home/stijn/Development/OpenWrt/openwrt/staging_dir/hostpkg/lib/ucode/*.so\", \"/home/stijn/Development/OpenWrt/openwrt/staging_dir/hostpkg/share/ucode/*.uc\", \"./*.so\", \"./*.uc\"" -DNDEBUG -D_FORTIFY_SOURCE=2 -D_GNU_SOURCE -Dlibucode_EXPORTS -I"/home/stijn/Development/OpenWrt/openwrt/build_dir/hostpkg/ucode-2025.02.10~a8a11aea/include" -I/home/stijn/Development/OpenWrt/openwrt/staging_dir/host/include/json-c -O2 -I/home/stijn/Development/OpenWrt/openwrt/staging_dir/host/include -I/home/stijn/Development/OpenWrt/openwrt/staging_dir/hostpkg/include -I/home/stijn/Development/OpenWrt/openwrt/staging_dir/target-aarch64_cortex-a53_musl/host/include -DNDEBUG -fPIC   -Os -Wall -Werror --std=gnu99 -ffunction-sections -fwrapv -Wextra -Werror=implicit-function-declaration -Wformat -Werror=format-security -Werror=format-nonliteral -Wmissing-declarations -Wno-error=unused-variable -Wno-unused-parameter -MD -MT CMakeFiles/libucode.dir/platform.c.o -MF CMakeFiles/libucode.dir/platform.c.o.d -o CMakeFiles/libucode.dir/platform.c.o -c '/home/stijn/Development/OpenWrt/openwrt/build_dir/hostpkg/ucode-2025.02.10~a8a11aea/platform.c'
FAILED: CMakeFiles/libucode.dir/platform.c.o 
/home/stijn/Development/OpenWrt/openwrt/staging_dir/host/bin/gcc -DHAVE_ARRAY_EXT -DHAVE_JSON_UINT64 -DHAVE_PARSE_END -DLIB_SEARCH_PATH="\"/home/stijn/Development/OpenWrt/openwrt/staging_dir/hostpkg/lib/ucode/*.so\", \"/home/stijn/Development/OpenWrt/openwrt/staging_dir/hostpkg/share/ucode/*.uc\", \"./*.so\", \"./*.uc\"" -DNDEBUG -D_FORTIFY_SOURCE=2 -D_GNU_SOURCE -Dlibucode_EXPORTS -I"/home/stijn/Development/OpenWrt/openwrt/build_dir/hostpkg/ucode-2025.02.10~a8a11aea/include" -I/home/stijn/Development/OpenWrt/openwrt/staging_dir/host/include/json-c -O2 -I/home/stijn/Development/OpenWrt/openwrt/staging_dir/host/include -I/home/stijn/Development/OpenWrt/openwrt/staging_dir/hostpkg/include -I/home/stijn/Development/OpenWrt/openwrt/staging_dir/target-aarch64_cortex-a53_musl/host/include -DNDEBUG -fPIC   -Os -Wall -Werror --std=gnu99 -ffunction-sections -fwrapv -Wextra -Werror=implicit-function-declaration -Wformat -Werror=format-security -Werror=format-nonliteral -Wmissing-declarations -Wno-error=unused-variable -Wno-unused-parameter -MD -MT CMakeFiles/libucode.dir/platform.c.o -MF CMakeFiles/libucode.dir/platform.c.o.d -o CMakeFiles/libucode.dir/platform.c.o -c '/home/stijn/Development/OpenWrt/openwrt/build_dir/hostpkg/ucode-2025.02.10~a8a11aea/platform.c'
<command-line>: error: "_FORTIFY_SOURCE" redefined [-Werror]
<built-in>: note: this is the location of the previous definition
cc1: all warnings being treated as errors

I suspect this is due to host gcc enabling _FORTIFY_SOURCE=3 by default, so the check added in cfb24ea doesn't work.

ossf/wg-best-practices-os-developers#659 contains some useful info and links about the problem.

Full build log: logspackageutilsucodehost-compile.txt

@jow-
Copy link
Owner

jow- commented Mar 10, 2025

Please try this patch:

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 858557e..c3bc2cb 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -1,6 +1,7 @@
 cmake_minimum_required(VERSION 3.13)
 include(CheckFunctionExists)
 include(CheckSymbolExists)
+include(CheckCSourceCompiles)
 
 project(ucode C)
 add_definitions(-Os -Wall -Werror --std=gnu99 -ffunction-sections -fwrapv -D_GNU_SOURCE)
@@ -10,7 +11,16 @@ if(CMAKE_C_COMPILER_VERSION VERSION_GREATER 6)
        add_definitions(-Wformat -Werror=format-security -Werror=format-nonliteral)
 endif()
 
-if(NOT CMAKE_C_FLAGS MATCHES -D_FORTIFY_SOURCE=)
+check_c_source_compiles("
+int main() {
+#ifndef _FORTIFY_SOURCE
+#error No fortification
+#endif
+       return 0;
+}
+" HAVE_FORTIFY_SOURCE)
+
+if(NOT HAVE_FORTIFY_SOURCE)
        add_definitions(-D_FORTIFY_SOURCE=2)
 endif()
 

@stintel
Copy link
Author

stintel commented Mar 10, 2025

That seems to work, thanks!

-- Performing Test HAVE_FORTIFY_SOURCE
-- Performing Test HAVE_FORTIFY_SOURCE - Success

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants