From ab41b3612aa10af5c46f14dff3dee8581fbe8607 Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Mon, 30 Sep 2024 10:43:00 +0200 Subject: [PATCH] lib: Change ADD_TO_ARRAY to use a tmp variable This should fix a few Coverity Resource Leak findings. Coverity does not understand that SMB_ASSERT aborts the program, so it believes if realloc fails we leak the previous allocation. Those are false positives, but doing it this way does not cost much. Signed-off-by: Volker Lendecke Reviewed-by: Andreas Schneider --- source3/include/smb_macros.h | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/source3/include/smb_macros.h b/source3/include/smb_macros.h index bd7f1a115ad..3192cbe11d8 100644 --- a/source3/include/smb_macros.h +++ b/source3/include/smb_macros.h @@ -273,23 +273,26 @@ copy an IP address from one buffer to another #endif -#define ADD_TO_ARRAY(mem_ctx, type, elem, array, num) \ - do { \ - *(array) = talloc_realloc(mem_ctx, \ - (*(array)), \ - type, \ - (*(num)) + 1); \ - SMB_ASSERT((*(array)) != NULL); \ - (*(array))[*(num)] = (elem); \ - (*(num)) += 1; \ +#define ADD_TO_ARRAY(mem_ctx, type, elem, array, num) \ + do { \ + type *__add_to_array_tmp = talloc_realloc(mem_ctx, \ + (*(array)), \ + type, \ + (*(num)) + 1); \ + SMB_ASSERT(__add_to_array_tmp != NULL); \ + __add_to_array_tmp[*(num)] = (elem); \ + (*(num)) += 1; \ + (*(array)) = __add_to_array_tmp; \ } while (0) -#define ADD_TO_MALLOC_ARRAY(type, elem, array, num) \ - do { \ - *(array) = SMB_REALLOC_ARRAY((*(array)), type, (*(num)) + 1); \ - SMB_ASSERT((*(array)) != NULL); \ - (*(array))[*(num)] = (elem); \ - (*(num)) += 1; \ +#define ADD_TO_MALLOC_ARRAY(type, elem, array, num) \ + do { \ + type *__add_to_malloc_array_tmp = SMB_REALLOC_ARRAY( \ + (*(array)), type, (*(num)) + 1); \ + SMB_ASSERT(__add_to_malloc_array_tmp != NULL); \ + __add_to_malloc_array_tmp[*(num)] = (elem); \ + (*(num)) += 1; \ + (*(array)) = __add_to_malloc_array_tmp; \ } while (0) #define ADD_TO_LARGE_ARRAY(mem_ctx, type, elem, array, num, size) \ -- 2.11.4.GIT