From: Martin Matuska Date: Tue, 31 Jan 2017 00:20:20 +0000 (+0100) Subject: Properly free memory allocated by acl_get_qualifier() X-Git-Tag: v3.3.0~29 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=190f798668f2f9747359b074c49c936f4f08e817;p=thirdparty%2Flibarchive.git Properly free memory allocated by acl_get_qualifier() Plug memory leaks in acl tests --- diff --git a/libarchive/archive_read_disk_entry_from_file.c b/libarchive/archive_read_disk_entry_from_file.c index 47085cb7b..8fb969a65 100644 --- a/libarchive/archive_read_disk_entry_from_file.c +++ b/libarchive/archive_read_disk_entry_from_file.c @@ -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: diff --git a/libarchive/test/test_acl_platform_nfs4.c b/libarchive/test/test_acl_platform_nfs4.c index be2826100..01c1dc589 100644 --- a/libarchive/test/test_acl_platform_nfs4.c +++ b/libarchive/test/test_acl_platform_nfs4.c @@ -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) { diff --git a/libarchive/test/test_acl_platform_posix1e.c b/libarchive/test/test_acl_platform_posix1e.c index 0c6a2c8f6..f3e1722f7 100644 --- a/libarchive/test/test_acl_platform_posix1e.c +++ b/libarchive/test/test_acl_platform_posix1e.c @@ -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 } diff --git a/libarchive/test/test_acl_text.c b/libarchive/test/test_acl_text.c index 2781e6ea5..23d991e40 100644 --- a/libarchive/test/test_acl_text.c +++ b/libarchive/test/test_acl_text.c @@ -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); }