From 80c708fdebebc95c6e861fbdb3f2506c31b71d68 Mon Sep 17 00:00:00 2001 From: Dirk Zimoch Date: Fri, 7 Nov 2025 16:43:02 +0100 Subject: [PATCH] add function attributes __malloc__ and __alloc_size__ for gcc and clang --- modules/libcom/src/misc/cantProceed.h | 5 +++-- modules/libcom/src/osi/compiler/clang/compilerSpecific.h | 5 +++++ modules/libcom/src/osi/compiler/gcc/compilerSpecific.h | 9 +++++++++ modules/libcom/src/osi/compilerDependencies.h | 4 ++++ 4 files changed, 21 insertions(+), 2 deletions(-) diff --git a/modules/libcom/src/misc/cantProceed.h b/modules/libcom/src/misc/cantProceed.h index 2150ead3f..910ace1fe 100644 --- a/modules/libcom/src/misc/cantProceed.h +++ b/modules/libcom/src/misc/cantProceed.h @@ -67,7 +67,7 @@ void cantProceed( * Will never return NULL otherwise. */ LIBCOM_API void * callocMustSucceed(size_t count, size_t size, - const char *errorMessage); + const char *errorMessage) EPICS_MALLOC(1,2); /** \brief A malloc() which suspends on error. * \param size Size of block to allocate. * \param errorMessage Context added to logged error message @@ -76,7 +76,8 @@ LIBCOM_API void * callocMustSucceed(size_t count, size_t size, * Will always return NULL for a zero length allocation. * Will never return NULL otherwise. */ -LIBCOM_API void * mallocMustSucceed(size_t size, const char *errorMessage); +LIBCOM_API void * mallocMustSucceed(size_t size, const char *errorMessage) + EPICS_MALLOC(1); /** @} */ #ifdef __cplusplus diff --git a/modules/libcom/src/osi/compiler/clang/compilerSpecific.h b/modules/libcom/src/osi/compiler/clang/compilerSpecific.h index bf2474a55..e6ae0cb2b 100644 --- a/modules/libcom/src/osi/compiler/clang/compilerSpecific.h +++ b/modules/libcom/src/osi/compiler/clang/compilerSpecific.h @@ -63,4 +63,9 @@ */ #define EPICS_NORETURN __attribute__((noreturn)) +/* + * malloc marker takes 1 or 2 args: (index of size) or (index of count, index of element size) + */ +#define EPICS_MALLOC(...) __attribute__((__malloc__, __alloc_size__(__VA_ARGS__))) + #endif /* ifndef compilerSpecific_h */ diff --git a/modules/libcom/src/osi/compiler/gcc/compilerSpecific.h b/modules/libcom/src/osi/compiler/gcc/compilerSpecific.h index 80b72012f..c5654f612 100644 --- a/modules/libcom/src/osi/compiler/gcc/compilerSpecific.h +++ b/modules/libcom/src/osi/compiler/gcc/compilerSpecific.h @@ -69,4 +69,13 @@ #define EPICS_NORETURN __attribute__((noreturn)) #endif +/* + * malloc marker takes 1 or 2 args: (index of size) or (index of count, index of element size) + */ +#if __GNUC__ * 100 + __GNUC_MINOR__ >= 403 +#define EPICS_MALLOC(...) __attribute__((__malloc__, __alloc_size__(__VA_ARGS__))) +#else +#define EPICS_MALLOC(...) __attribute__((__malloc__)) +#endif + #endif /* ifndef compilerSpecific_h */ diff --git a/modules/libcom/src/osi/compilerDependencies.h b/modules/libcom/src/osi/compilerDependencies.h index 55762ee20..d484a7fba 100644 --- a/modules/libcom/src/osi/compilerDependencies.h +++ b/modules/libcom/src/osi/compilerDependencies.h @@ -56,6 +56,10 @@ # define EPICS_NORETURN #endif +#ifndef EPICS_MALLOC +# define EPICS_MALLOC(...) +#endif + #ifndef EPICS_FUNCTION #if (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901)) || (defined(__cplusplus) && __cplusplus>=201103L) # define EPICS_FUNCTION __func__