]> git.ipfire.org Git - thirdparty/libarchive.git/commitdiff
zip: remove redundant helper 3301/head
authordatauwu <datauwu@users.noreply.github.com>
Sat, 18 Jul 2026 11:08:44 +0000 (19:08 +0800)
committerdatauwu <datauwu@users.noreply.github.com>
Sat, 18 Jul 2026 11:19:45 +0000 (19:19 +0800)
Calculate the pathname length directly after validating the path.

The helper only wrapped strlen() and adjusted the length for directory
entries. Removing it keeps the calculation beside the validation and
reuses the existing file type value.

libarchive/archive_write_set_format_zip.c

index 4e748af420116fc914784324c670e8b065a0f81d..ed00c848440c02a6cac832d1b9f1470036380718 100644 (file)
@@ -234,7 +234,6 @@ static int archive_write_zip_header(struct archive_write *,
              struct archive_entry *);
 static int archive_write_zip_options(struct archive_write *,
              const char *, const char *);
-static size_t path_length(struct archive_entry *);
 static int write_path(struct archive_entry *, struct archive_write *);
 static void copy_path(struct archive_entry *, unsigned char *);
 static struct archive_string_conv *get_sconv(struct archive_write *, struct zip *);
@@ -958,8 +957,6 @@ archive_write_zip_header(struct archive_write *a, struct archive_entry *entry)
 #endif
                }
        }
-       filename_length = path_length(zip->entry);
-
        /* Reject empty or overlong pathnames */
        path = archive_entry_pathname(zip->entry);
        if (path == NULL || path[0] == '\0') {
@@ -967,6 +964,10 @@ archive_write_zip_header(struct archive_write *a, struct archive_entry *entry)
                    "ZIP format requires a non-empty pathname");
                return (ARCHIVE_FAILED);
        }
+       filename_length = strlen(path);
+       /* Include the trailing slash added to directories. */
+       if (type == AE_IFDIR && path[filename_length - 1] != '/')
+               filename_length++;
        if (filename_length > 0xffff) {
                archive_set_error(&a->archive, ENAMETOOLONG,
                    "Pathname too long for ZIP format");
@@ -2278,24 +2279,6 @@ archive_write_zip_free(struct archive_write *a)
        return (ARCHIVE_OK);
 }
 
-static size_t
-path_length(struct archive_entry *entry)
-{
-       mode_t type;
-       const char *path;
-       size_t len;
-
-       type = archive_entry_filetype(entry);
-       path = archive_entry_pathname(entry);
-
-       if (path == NULL)
-               return (0);
-       len = strlen(path);
-       if (type == AE_IFDIR && (path[0] == '\0' || path[len - 1] != '/'))
-               ++len; /* Space for the trailing / */
-       return len;
-}
-
 static int
 write_path(struct archive_entry *entry, struct archive_write *archive)
 {