From 679d82c48411973a477da05d4e77fca875902e73 Mon Sep 17 00:00:00 2001 From: Grant Ramsay Date: Sat, 11 Nov 2023 09:35:49 +1300 Subject: [PATCH] libc: Add GCC fno-builtin-malloc flag to common stdlib compilation This prevents the compiler from optimizing calloc into an infinite recursive call. For example a call to malloc + memset zero at GCC -O2 will be replaced by a call to calloc. This causes infinite recursion if the function being implemented *is* calloc. fno-builtin-malloc forces the compiler to avoid this optimization. Signed-off-by: Grant Ramsay --- lib/libc/common/CMakeLists.txt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/libc/common/CMakeLists.txt b/lib/libc/common/CMakeLists.txt index 48372158ea5..3aa869379d5 100644 --- a/lib/libc/common/CMakeLists.txt +++ b/lib/libc/common/CMakeLists.txt @@ -6,3 +6,6 @@ zephyr_library_sources_ifdef(CONFIG_COMMON_LIBC_ABORT source/stdlib/abort.c) zephyr_library_sources_ifdef(CONFIG_COMMON_LIBC_TIME source/time/time.c) zephyr_library_sources_ifdef(CONFIG_COMMON_LIBC_MALLOC source/stdlib/malloc.c) zephyr_library_sources_ifdef(CONFIG_COMMON_LIBC_STRNLEN source/string/strnlen.c) + +# Prevent compiler from optimizing calloc into an infinite recursive call +zephyr_library_cc_option(-fno-builtin-malloc)