]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
lib: Change ADD_TO_ARRAY to use a tmp variable
authorVolker Lendecke <vl@samba.org>
Mon, 30 Sep 2024 08:43:00 +0000 (10:43 +0200)
committerVolker Lendecke <vl@samba.org>
Wed, 2 Oct 2024 13:12:30 +0000 (13:12 +0000)
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 <vl@samba.org>
Reviewed-by: Andreas Schneider <asn@samba.org>
source3/include/smb_macros.h

index bd7f1a115ad391382d833078a0da4bc6ffa45102..3192cbe11d82ad6e95e0cc852ceadf2d653b34c1 100644 (file)
@@ -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) \