From 14a5336802450d8c0864ba64c302e591e0389a19 Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Mon, 30 Sep 2024 10:34:17 +0200 Subject: [PATCH] lib: Factor out ADD_TO_MALLOC_ARRAY() ADD_TO_ARRAY with an explicit NULL mem_ctx is only used in 3 places. I've checked the other places, and I think I made sure that the mem_ctx being passed in is non-NULL everywhere else. This makes the "legacy" use with SMB_REALLOC more obvious. Signed-off-by: Volker Lendecke Reviewed-by: Andreas Schneider --- source3/include/smb_macros.h | 27 ++++++++++++++++++--------- source3/modules/vfs_hpuxacl.c | 3 +-- source3/modules/vfs_solarisacl.c | 6 ++++-- source3/utils/net_rpc_shell.c | 4 ++-- 4 files changed, 25 insertions(+), 15 deletions(-) diff --git a/source3/include/smb_macros.h b/source3/include/smb_macros.h index 4b3989dce93..bd7f1a115ad 100644 --- a/source3/include/smb_macros.h +++ b/source3/include/smb_macros.h @@ -273,15 +273,24 @@ copy an IP address from one buffer to another #endif -#define ADD_TO_ARRAY(mem_ctx, type, elem, array, num) \ -do { \ - *(array) = ((mem_ctx) != NULL) ? \ - talloc_realloc(mem_ctx, (*(array)), type, (*(num))+1) : \ - SMB_REALLOC_ARRAY((*(array)), type, (*(num))+1); \ - SMB_ASSERT((*(array)) != NULL); \ - (*(array))[*(num)] = (elem); \ - (*(num)) += 1; \ -} while (0) +#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; \ + } 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; \ + } while (0) #define ADD_TO_LARGE_ARRAY(mem_ctx, type, elem, array, num, size) \ add_to_large_array((mem_ctx), sizeof(type), &(elem), (void *)(array), (num), (size)); diff --git a/source3/modules/vfs_hpuxacl.c b/source3/modules/vfs_hpuxacl.c index 31903a1feca..91801daca98 100644 --- a/source3/modules/vfs_hpuxacl.c +++ b/source3/modules/vfs_hpuxacl.c @@ -727,8 +727,7 @@ static bool hpux_add_to_acl(HPUX_ACL_T *hpux_acl, int *count, if (!_IS_OF_TYPE(add_acl[i], type)) { continue; } - ADD_TO_ARRAY(NULL, HPUX_ACE_T, add_acl[i], - hpux_acl, count); + ADD_TO_MALLOC_ARRAY(HPUX_ACE_T, add_acl[i], hpux_acl, count); if (hpux_acl == NULL) { DEBUG(10, ("error enlarging acl.\n")); errno = ENOMEM; diff --git a/source3/modules/vfs_solarisacl.c b/source3/modules/vfs_solarisacl.c index d31bda50233..eb54c618315 100644 --- a/source3/modules/vfs_solarisacl.c +++ b/source3/modules/vfs_solarisacl.c @@ -609,8 +609,10 @@ static bool solaris_add_to_acl(SOLARIS_ACL_T *solaris_acl, int *count, if (!_IS_OF_TYPE(add_acl[i], type)) { continue; } - ADD_TO_ARRAY(NULL, SOLARIS_ACE_T, add_acl[i], - solaris_acl, count); + ADD_TO_MALLOC_ARRAY(SOLARIS_ACE_T, + add_acl[i], + solaris_acl, + count); if (solaris_acl == NULL) { DEBUG(10, ("error enlarging acl.\n")); errno = ENOMEM; diff --git a/source3/utils/net_rpc_shell.c b/source3/utils/net_rpc_shell.c index 1ea7080e35d..5b1285086f8 100644 --- a/source3/utils/net_rpc_shell.c +++ b/source3/utils/net_rpc_shell.c @@ -52,7 +52,7 @@ static char **completion_fn(const char *text, int start, int end) return NULL; } - ADD_TO_ARRAY(NULL, char *, SMB_STRDUP(text), &cmds, &n_cmds); + ADD_TO_MALLOC_ARRAY(char *, SMB_STRDUP(text), &cmds, &n_cmds); for (c = this_ctx->cmds; c->name != NULL; c++) { bool match = (strncmp(text, c->name, strlen(text)) == 0); @@ -69,7 +69,7 @@ static char **completion_fn(const char *text, int start, int end) n_cmds -= 1; } - ADD_TO_ARRAY(NULL, char *, NULL, &cmds, &n_cmds); + ADD_TO_MALLOC_ARRAY(char *, NULL, &cmds, &n_cmds); return cmds; } -- 2.47.3