]> git.ipfire.org Git - thirdparty/libarchive.git/commitdiff
Properly free memory allocated by acl_get_qualifier()
authorMartin Matuska <martin@matuska.org>
Tue, 31 Jan 2017 00:20:20 +0000 (01:20 +0100)
committerMartin Matuska <martin@matuska.org>
Tue, 31 Jan 2017 02:14:04 +0000 (03:14 +0100)
Plug memory leaks in acl tests

libarchive/archive_read_disk_entry_from_file.c
libarchive/test/test_acl_platform_nfs4.c
libarchive/test/test_acl_platform_posix1e.c
libarchive/test/test_acl_text.c

index 47085cb7b9ada947fdd7a91d029fc17b6084d4f9..8fb969a65434d70943a571015c07993bf46f0516 100644 (file)
@@ -731,8 +731,10 @@ static int translate_guid(struct archive *a, acl_entry_t acl_entry,
        if (q == NULL)
                return (1);
        r = mbr_uuid_to_id((const unsigned char *)q, &ugid, &idtype);
-       if (r != 0)
+       if (r != 0) {
+               acl_free(q);
                return (1);
+       }
        if (idtype == ID_TYPE_UID) {
                *ae_tag = ARCHIVE_ENTRY_ACL_USER;
                pwd = getpwuuid(q);
@@ -754,8 +756,10 @@ static int translate_guid(struct archive *a, acl_entry_t acl_entry,
                        *ae_name = archive_read_disk_gname(a, *ae_id);
                }
        } else
-               return (1);
-       return (0);
+               r = 1;
+
+       acl_free(q);
+       return (r);
 }
 
 /*
@@ -1155,6 +1159,9 @@ translate_acl(struct archive_read_disk *a,
        acl_permset_t    acl_permset;
        int              i, entry_acl_type;
        int              r, s, ae_id, ae_tag, ae_perm;
+#if !HAVE_DARWIN_ACL
+       void            *q;
+#endif
        const char      *ae_name;
 
 #if HAVE_ACL_TYPE_NFS4
@@ -1217,13 +1224,23 @@ translate_acl(struct archive_read_disk *a,
                switch (acl_tag) {
 #if !HAVE_DARWIN_ACL   /* FreeBSD, Linux */
                case ACL_USER:
-                       ae_id = (int)*(uid_t *)acl_get_qualifier(acl_entry);
-                       ae_name = archive_read_disk_uname(&a->archive, ae_id);
+                       q = acl_get_qualifier(acl_entry);
+                       if (q != NULL) {
+                               ae_id = (int)*(uid_t *)q;
+                               acl_free(q);
+                               ae_name = archive_read_disk_uname(&a->archive,
+                                   ae_id);
+                       }
                        ae_tag = ARCHIVE_ENTRY_ACL_USER;
                        break;
                case ACL_GROUP:
-                       ae_id = (int)*(gid_t *)acl_get_qualifier(acl_entry);
-                       ae_name = archive_read_disk_gname(&a->archive, ae_id);
+                       q = acl_get_qualifier(acl_entry);
+                       if (q != NULL) {
+                               ae_id = (int)*(gid_t *)q;
+                               acl_free(q);
+                               ae_name = archive_read_disk_gname(&a->archive,
+                                   ae_id);
+                       }
                        ae_tag = ARCHIVE_ENTRY_ACL_GROUP;
                        break;
                case ACL_MASK:
index be282610009d686d2c94e13cdb031f8147b5e214..01c1dc589f6f7209ae5edcef2eabcc4258bad8d1 100644 (file)
@@ -488,6 +488,7 @@ acl_match(acl_entry_t aclent, struct myacl_t *myacl)
        if (q == NULL)
                return (0);
        r = mbr_uuid_to_id((const unsigned char *)q, &ugid, &idtype);
+       acl_free(q);
        if (r != 0)
                return (0);
        switch (idtype) {
index 0c6a2c8f67362a87d16ea1433d2cd0fbf4dd5c93..f3e1722f711d6329b129d6aecd41d303472e55b9 100644 (file)
@@ -429,7 +429,7 @@ DEFINE_TEST(test_acl_platform_posix1e_read)
        struct archive *a;
        struct archive_entry *ae;
        int n, fd, flags, dflags;
-       char *func;
+       char *func, *acl_text;
        const char *acl1_text, *acl2_text, *acl3_text;
 #if HAVE_SUN_ACL
        acl_t *acl, *acl1, *acl2, *acl3;
@@ -498,6 +498,8 @@ DEFINE_TEST(test_acl_platform_posix1e_read)
        func = "acl_set_fd()";
        n = acl_set_fd(fd, acl1);
 #endif
+       acl_free(acl1);
+
        if (n != 0) {
 #if HAVE_SUN_ACL
                if (errno == ENOSYS)
@@ -562,10 +564,9 @@ DEFINE_TEST(test_acl_platform_posix1e_read)
        func = "acl_set_fd()";
        n = acl_set_fd(fd, acl2);
 #endif
-       if (n != 0) {
-               acl_free(acl2);
+       acl_free(acl2);
+       if (n != 0)
                close(fd);
-       }
        failure("%s: errno = %d (%s)", func, errno, strerror(errno));
        assertEqualInt(0, n);
        close(fd);
@@ -608,8 +609,8 @@ DEFINE_TEST(test_acl_platform_posix1e_read)
        func = "acl_set_file()";
        n = acl_set_file("d2", ACL_TYPE_DEFAULT, acl3);
 #endif
-       if (n != 0)
-               acl_free(acl3);
+       acl_free(acl3);
+
        failure("%s: errno = %d (%s)", func, errno, strerror(errno));
        assertEqualInt(0, n);
 
@@ -632,15 +633,21 @@ DEFINE_TEST(test_acl_platform_posix1e_read)
        while (ARCHIVE_OK == archive_read_next_header2(a, ae)) {
                archive_read_disk_descend(a);
                if (strcmp(archive_entry_pathname(ae), "./f1") == 0) {
-                       assertEqualString(archive_entry_acl_to_text(ae, NULL, flags), acl1_text);
-                           
+                       acl_text = archive_entry_acl_to_text(ae, NULL, flags);
+                       assertEqualString(acl_text, acl1_text);
+                       free(acl_text);
                } else if (strcmp(archive_entry_pathname(ae), "./d/f1") == 0) {
-                       assertEqualString(archive_entry_acl_to_text(ae, NULL, flags), acl2_text);
+                       acl_text = archive_entry_acl_to_text(ae, NULL, flags);
+                       assertEqualString(acl_text, acl2_text);
+                       free(acl_text);
                } else if (strcmp(archive_entry_pathname(ae), "./d2") == 0) {
-                       assertEqualString(archive_entry_acl_to_text(ae, NULL, dflags), acl3_text);
+                       acl_text = archive_entry_acl_to_text(ae, NULL, dflags);
+                       assertEqualString(acl_text, acl3_text);
+                       free(acl_text);
                }
        }
 
-       archive_free(a);
+       archive_entry_free(ae);
+       assertEqualInt(ARCHIVE_OK, archive_free(a));
 #endif
 }
index 2781e6ea56631c6e6bd5b63cae95c2857f10cefa..23d991e403381ad35515611c4699d2383004983f 100644 (file)
@@ -242,8 +242,8 @@ convert_s_to_ws(const char *s)
 static void
 compare_acl_text(struct archive_entry *ae, int flags, const char *s)
 {
-       const char *text;
-       const wchar_t *wtext;
+       char *text;
+       wchar_t *wtext;
        wchar_t *ws;
        ssize_t slen;
 
@@ -257,9 +257,10 @@ compare_acl_text(struct archive_entry *ae, int flags, const char *s)
        assertEqualWString(wtext, ws);
        if (wtext != NULL) {
                assertEqualInt(wcslen(wtext), slen);
-               free(ws);
-               ws = NULL;
        }
+       free(text);
+       free(wtext);
+       free(ws);
 }
 
 DEFINE_TEST(test_acl_from_text)
@@ -395,6 +396,9 @@ DEFINE_TEST(test_acl_from_text)
        assertEqualInt(6, archive_entry_acl_reset(ae,
            ARCHIVE_ENTRY_ACL_TYPE_NFS4));
        archive_entry_acl_clear(ae);
+
+       free(ws);
+       archive_entry_free(ae);
 }
 
 DEFINE_TEST(test_acl_to_text)
@@ -453,4 +457,6 @@ DEFINE_TEST(test_acl_to_text)
 
        /* NFSv4 ACLs like "getfacl -i" on FreeBSD */
        compare_acl_text(ae, ARCHIVE_ENTRY_ACL_STYLE_EXTRA_ID, acltext[10]);
+
+       archive_entry_free(ae);
 }