From 07286647fb7446a62e8690675797ba7426b0758b Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Mon, 22 Sep 2003 18:54:50 +0000 Subject: [PATCH] --strip strips file system prefix too, and it counts adjacent slashes as if they were one slash. --- ChangeLog | 12 ++++++++++++ src/common.h | 2 +- src/extract.c | 5 +++-- src/names.c | 24 ++++++++++++++++-------- 4 files changed, 32 insertions(+), 11 deletions(-) diff --git a/ChangeLog b/ChangeLog index f990a35..af6888b 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,15 @@ +2003-09-22 Paul Eggert + + * doc/tar.texi (extracting untrusted archives): New section. + + * src/common.h (stripped_path_len): Renamed from cut_path_elements. + Return size_t, not pointer, so that we don't have to worry about + violating the C standard by converting char const * to char *. + All callers changed. + * src/names.c (stripped_path_len): Likewise. Strip file system + prefix, too. Count adjacent slashes as if they were one slash; + that is the POSIX standard. + 2003-09-17 Paul Eggert * README-alpha: Document maintainer tool assumptions a bit. GNU diff --git a/src/common.h b/src/common.h index 40609bb..9e1ec68 100644 --- a/src/common.h +++ b/src/common.h @@ -567,7 +567,7 @@ char *name_from_list (void); void blank_name_list (void); char *new_name (const char *, const char *); char *safer_name_suffix (char const *, bool); -char const *cut_path_elements (char const *file_name, size_t num); +size_t stripped_path_len (char const *file_name, size_t num); bool excluded_name (char const *); diff --git a/src/extract.c b/src/extract.c index d7aba0d..26fcecb 100644 --- a/src/extract.c +++ b/src/extract.c @@ -719,12 +719,13 @@ extract_archive (void) file_name = safer_name_suffix (current_stat_info.file_name, 0); if (strip_path_elements) { - file_name = cut_path_elements (file_name, strip_path_elements); - if (!file_name) + size_t prefix_len = stripped_prefix_len (file_name, strip_path_elements); + if (prefix_len == (size_t) -1) { skip_member (); return; } + file_name += prefix_len; } apply_nonancestor_delayed_set_stat (file_name, 0); diff --git a/src/names.c b/src/names.c index ee68749..04719d0 100644 --- a/src/names.c +++ b/src/names.c @@ -1043,21 +1043,29 @@ safer_name_suffix (char const *file_name, bool link_target) return (char *) p; } -char const * -cut_path_elements (char const *file_name, size_t num) +/* Return the size of the prefix of FILE_NAME that is removed after + stripping NUM leading path name components. NUM must be + positive. */ + +size_t +stripped_prefix_len (char const *file_name, size_t num) { - char const *p = file_name; - if (ISSLASH (*p)) + char const *p = file_name + FILESYSTEM_PREFIX_LEN (file_name); + while (ISSLASH (*p)) p++; - for (; *p; p++) + while (*p) { - if (ISSLASH (*p)) + bool slash = ISSLASH (*p); + p++; + if (slash) { if (--num == 0) - return p + 1; + return p - file_name; + while (ISSLASH (*p)) + p++; } } - return NULL; + return -1; } /* Return nonzero if NAME contains ".." as a path name component. */ -- 2.45.2