librc: mark stringlist functions as mallocs

This gives a hint to the compiler that allocations (return values)
from this function should be paired with a corresponding dealloc/free
function

In this case, it means that every rc_stringlist that rc_stringlist_new()
returns should eventually be freed by calling rc_stringlist_free(ptr)
where ptr is the relevant rc_stringlist.

We have to add a test for this into the build system
because only GCC supports this for now. In future, we might
be able to use meson's has_function_attribute (it does support
'malloc', just not AFAICT 'malloc with arguments').

Signed-off-by: Sam James <sam@gentoo.org>
This commit is contained in:
Sam James 2022-03-31 07:25:35 +01:00 committed by William Hubbs
parent 1afcc37803
commit 17de4e5dfd
2 changed files with 18 additions and 5 deletions

View File

@ -182,6 +182,16 @@ cc_warning_flags = cc.get_supported_arguments(cc_warning_flags_test)
cc_flags = [cc_debug_flags, cc_os_flags, cc_warning_flags] cc_flags = [cc_debug_flags, cc_os_flags, cc_warning_flags]
add_project_arguments(cc_flags, language : 'c') add_project_arguments(cc_flags, language : 'c')
# Meson's has_function_attribute doesn't know about GCC's extended
# version (with arguments), so we have to build a test program instead.
malloc_attribute_test = '''#include<stdlib.h>
__attribute__ ((malloc (free, 1)))
int func() { return 0; }
'''
if cc.compiles(malloc_attribute_test, name : 'malloc attribute with arguments')
add_project_arguments('-DHAVE_MALLOC_EXTENDED_ATTRIBUTE', language: 'c')
endif
incdir = include_directories('src/shared') incdir = include_directories('src/shared')
einfo_incdir = include_directories('src/libeinfo') einfo_incdir = include_directories('src/libeinfo')
rc_incdir = include_directories('src/librc') rc_incdir = include_directories('src/librc')

View File

@ -528,8 +528,15 @@ bool rc_yesno(const char *);
/*! @name String List functions /*! @name String List functions
* Every string list should be released with a call to rc_stringlist_free. */ * Every string list should be released with a call to rc_stringlist_free. */
/*! Create a new stringlist /*! Frees each item on the list and the list itself.
* @param list to free */
void rc_stringlist_free(RC_STRINGLIST *);
/*! Create a new stringlinst
* @return pointer to new list */ * @return pointer to new list */
#ifdef HAVE_MALLOC_EXTENDED_ATTRIBUTE
__attribute__ ((malloc (rc_stringlist_free, 1)))
#endif
RC_STRINGLIST *rc_stringlist_new(void); RC_STRINGLIST *rc_stringlist_new(void);
/*! Duplicate the item, add it to end of the list and return a pointer to it. /*! Duplicate the item, add it to end of the list and return a pointer to it.
@ -568,10 +575,6 @@ RC_STRINGLIST *rc_stringlist_split(const char *, const char *);
* @param list to sort */ * @param list to sort */
void rc_stringlist_sort(RC_STRINGLIST **); void rc_stringlist_sort(RC_STRINGLIST **);
/*! Frees each item on the list and the list itself.
* @param list to free */
void rc_stringlist_free(RC_STRINGLIST *);
typedef struct rc_pid typedef struct rc_pid
{ {
pid_t pid; pid_t pid;