From: Paul Eggert Date: Wed, 22 Jul 2026 23:37:35 +0000 (-0700) Subject: Save a syscall in trivial extract_dir X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=25d8b4b1ac58b86ba7bf2db17da41399516d6db6;p=thirdparty%2Ftar.git Save a syscall in trivial extract_dir * src/extract.c (trivial_base_name): New function. (extract_dir): Use it. --- diff --git a/src/extract.c b/src/extract.c index b79b61a9..5febf6de 100644 --- a/src/extract.c +++ b/src/extract.c @@ -1176,6 +1176,19 @@ safe_dir_mode (struct stat const *st) | (we_are_root ? 0 : MODE_WXUSR)); } + +/* Return true if the base name BASE returned by fdbase corresponds to + a file that trivially exists. This is true if BASE is the empty + string (which means the original name is a file system root); or if + BASE is "." or "..", possibly followed by slashes. */ +static bool +trivial_base_name (char const *base) +{ + bool dotted = base[0] == '.'; + char const *p = base + dotted + (dotted & (base[dotted] == '.')); + return !*p | ISSLASH (*p); +} + /* Extractor functions for various member types */ static bool @@ -1210,7 +1223,17 @@ extract_dir (char *file_name, char UNNAMED (typeflag)) for (;;) { struct fdbase f = fdbase (file_name); - status = f.fd == BADFD ? -1 : mkdirat (f.fd, f.base, mode); + if (f.fd == BADFD) + status = -1; + else if (trivial_base_name (f.base)) + { + /* Save a syscall. */ + errno = EEXIST; + status = -1; + } + else + status = mkdirat (f.fd, f.base, mode); + if (status == 0) { current_mode = mode & ~ current_umask;