]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
lib: Simplify sys_acl_to_text() with talloc_asprintf_addbuf()
authorVolker Lendecke <vl@samba.org>
Tue, 16 Dec 2025 14:23:17 +0000 (15:23 +0100)
committerVolker Lendecke <vl@samba.org>
Wed, 7 Jan 2026 09:57:40 +0000 (09:57 +0000)
Signed-off-by: Volker Lendecke <vl@samba.org>
Reviewed-by: Anoop C S <anoopcs@samba.org>
source3/include/smb_acls.h
source3/lib/sysacls.c
source3/modules/vfs_posixacl.c
source3/torture/cmd_vfs.c

index 97b7a5333e8d6078a9d5f7eb27e0101daff2120e..f8e04ef884f23aa4fdb9fd472e636bc08d603e34 100644 (file)
@@ -50,7 +50,7 @@ void *sys_acl_get_qualifier(SMB_ACL_ENTRY_T entry_d);
 int sys_acl_clear_perms(SMB_ACL_PERMSET_T permset_d);
 int sys_acl_add_perm(SMB_ACL_PERMSET_T permset_d, SMB_ACL_PERM_T perm);
 int sys_acl_get_perm(SMB_ACL_PERMSET_T permset_d, SMB_ACL_PERM_T perm);
-char *sys_acl_to_text(const struct smb_acl_t *acl_d, ssize_t *len_p);
+char *sys_acl_to_text(TALLOC_CTX *mem_ctx, const struct smb_acl_t *acl_d);
 SMB_ACL_T sys_acl_init(TALLOC_CTX *mem_ctx);
 int sys_acl_create_entry(SMB_ACL_T *acl_p, SMB_ACL_ENTRY_T *entry_p);
 int sys_acl_set_tag_type(SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T tag_type);
index 234094a011b93695dc786c4bfa39cb708bb0b222..b00043b65c8ef122daa46b4b52050903fdcd1183 100644 (file)
@@ -143,23 +143,12 @@ int sys_acl_get_perm(SMB_ACL_PERMSET_T permset_d, SMB_ACL_PERM_T perm)
        return *permset_d & perm;
 }
 
-char *sys_acl_to_text(const struct smb_acl_t *acl_d, ssize_t *len_p)
+char *sys_acl_to_text(TALLOC_CTX *mem_ctx, const struct smb_acl_t *acl_d)
 {
-       int     i;
-       int     len, maxlen;
-       char    *text;
-
-       /*
-        * use an initial estimate of 20 bytes per ACL entry
-        * when allocating memory for the text representation
-        * of the ACL
-        */
-       len     = 0;
-       maxlen  = 20 * acl_d->count;
-       if ((text = (char *)SMB_MALLOC(maxlen)) == NULL) {
-               errno = ENOMEM;
-               return NULL;
-       }
+       int32_t i;
+       char *text = NULL;
+
+       text = talloc_strdup(mem_ctx, "");
 
        for (i = 0; i < acl_d->count; i++) {
                struct smb_acl_entry *ap = &acl_d->acl[i];
@@ -169,7 +158,6 @@ char *sys_acl_to_text(const struct smb_acl_t *acl_d, ssize_t *len_p)
                const char      *tag;
                const char      *id     = "";
                char            perms[4];
-               int             nbytes;
 
                switch (ap->a_type) {
                        /*
@@ -220,31 +208,9 @@ char *sys_acl_to_text(const struct smb_acl_t *acl_d, ssize_t *len_p)
                perms[2] = (ap->a_perm & SMB_ACL_EXECUTE) ? 'x' : '-';
                perms[3] = '\0';
 
-               /*          <tag>      :  <qualifier>   :  rwx \n  \0 */
-               nbytes = strlen(tag) + 1 + strlen(id) + 1 + 3 + 1 + 1;
-
-               /*
-                * If this entry would overflow the buffer
-                * allocate enough additional memory for this
-                * entry and an estimate of another 20 bytes
-                * for each entry still to be processed
-                */
-               if ((len + nbytes) > maxlen) {
-                       maxlen += nbytes + 20 * (acl_d->count - i);
-                       if ((text = (char *)SMB_REALLOC(text, maxlen)) == NULL) {
-                               errno = ENOMEM;
-                               return NULL;
-                       }
-               }
-
-
-               slprintf(&text[len], nbytes, "%s:%s:%s\n", tag, id, perms);
-               len += (nbytes - 1);
+               talloc_asprintf_addbuf(&text, "%s:%s:%s\n", tag, id, perms);
        }
 
-       if (len_p)
-               *len_p = len;
-
        return text;
 }
 
index 4eb1326bb7f1de3ebf34a3be15b8b1b033f89c9e..6be8f1cbc7a4824bc688ecb71c4e40dbb70669e3 100644 (file)
@@ -356,10 +356,11 @@ static acl_t smb_acl_to_posix(const struct smb_acl_t *acl)
        }
 
        if (acl_valid(result) != 0) {
-               char *acl_string = sys_acl_to_text(acl, NULL);
-               DEBUG(0, ("smb_acl_to_posix: ACL %s is invalid for set (%s)\n",
-                         acl_string, strerror(errno)));
-               SAFE_FREE(acl_string);
+               char *acl_string = sys_acl_to_text(talloc_tos(), acl);
+               DBG_WARNING("ACL %s is invalid for set (%s)\n",
+                           acl_string,
+                           strerror(errno));
+               TALLOC_FREE(acl_string);
                goto fail;
        }
 
index 4ae1e4e1931fd751826b818ad0f04253f2134560..acd053694f0cd19439fc1c4616df1eedf003e67d 100644 (file)
@@ -1859,10 +1859,9 @@ static NTSTATUS cmd_sys_acl_get_fd(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
                printf("sys_acl_get_fd failed (%s)\n", strerror(errno));
                return NT_STATUS_UNSUCCESSFUL;
        }
-       acl_text = sys_acl_to_text(acl, NULL);
+       acl_text = sys_acl_to_text(acl, acl);
        printf("%s", acl_text);
        TALLOC_FREE(acl);
-       SAFE_FREE(acl_text);
        return NT_STATUS_OK;
 }
 
@@ -1908,12 +1907,11 @@ static NTSTATUS cmd_sys_acl_get_file(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
                TALLOC_FREE(pathref_fname);
                return NT_STATUS_UNSUCCESSFUL;
        }
-       acl_text = sys_acl_to_text(acl, NULL);
+       acl_text = sys_acl_to_text(acl, acl);
        printf("%s", acl_text);
        TALLOC_FREE(acl);
        TALLOC_FREE(smb_fname);
        TALLOC_FREE(pathref_fname);
-       SAFE_FREE(acl_text);
        return NT_STATUS_OK;
 }