From 9d5289ae9ee38a70bf3c94b57aacd72e4165f75b Mon Sep 17 00:00:00 2001 From: =?utf8?q?Martin=20Matu=C5=A1ka?= Date: Mon, 7 Oct 2024 05:31:38 +0200 Subject: [PATCH] Fix pathname overwrite in header_old_tar() (#2360) Fixes #2359 --- libarchive/archive_read_support_format_tar.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/libarchive/archive_read_support_format_tar.c b/libarchive/archive_read_support_format_tar.c index 4f42db8f0..5517198ed 100644 --- a/libarchive/archive_read_support_format_tar.c +++ b/libarchive/archive_read_support_format_tar.c @@ -1520,9 +1520,17 @@ header_old_tar(struct archive_read *a, struct tar *tar, const struct archive_entry_header_ustar *header; int err = ARCHIVE_OK, err2; - /* Copy filename over (to ensure null termination). */ + /* + * Copy filename over (to ensure null termination). + * Skip if pathname was already set e.g. by header_gnu_longname() + */ header = (const struct archive_entry_header_ustar *)h; - if (archive_entry_copy_pathname_l(entry, + + const char *existing_pathname = archive_entry_pathname(entry); + const wchar_t *existing_wcs_pathname = archive_entry_pathname_w(entry); + if ((existing_pathname == NULL || existing_pathname[0] == '\0') + && (existing_wcs_pathname == NULL || existing_wcs_pathname[0] == '\0') && + archive_entry_copy_pathname_l(entry, header->name, sizeof(header->name), tar->sconv) != 0) { err = set_conversion_failed_error(a, tar->sconv, "Pathname"); if (err == ARCHIVE_FATAL) -- 2.47.2