]> git.ipfire.org Git - thirdparty/tar.git/commitdiff
tar: invalidate directory cache after failed open master
authorDarren Carreras <artemis@perpetualsai.com>
Thu, 6 Aug 2026 05:30:20 +0000 (08:30 +0300)
committerSergey Poznyakoff <gray@gnu.org>
Thu, 6 Aug 2026 05:35:37 +0000 (08:35 +0300)
fdbase_opendir copies a requested directory name into the cache before
opening it.  If open_subdir fails, this leaves the new name associated
with the old file descriptor.  A retry after tar creates the missing
parent can then extract into the old directory.

Close an unowned old descriptor before trying to open the new dir and
invalidate the cache entry if that fails.  Add a regression test using
equal-length sibling directory names.

* src/misc.c (fdbase_opendir): Invalidate the cache after a failed
replacement open.
* tests/extrac35.at: New test.
* tests/Makefile.am (TESTSUITE_AT): Add it.
* tests/testsuite.at: Include it.

src/misc.c
tests/Makefile.am
tests/extrac35.at [new file with mode: 0644]
tests/testsuite.at

index ad9d8335cc46905a7c619b03a7960ccaad0f2b12..3988aa5af588f29c33c20b10d60811c1c5b2cb75 100644 (file)
@@ -1377,14 +1377,19 @@ fdbase_opendir (char const *file_name, bool alternate, int child_oflags)
                }
            }
 
                }
            }
 
+         /* Remove any old directory info, and add new info if the new
+            directory can be opened.  */
+         if (0 < c->subdirlen && !chdirable (fd))
+           close (fd);
+
          int newfd = open_subdir (chdir_fd, c->subdir, child_oflags);
          if (newfd < 0)
          int newfd = open_subdir (chdir_fd, c->subdir, child_oflags);
          if (newfd < 0)
-           fd = BADFD == -1 ? newfd : BADFD;
+           {
+             c->subdirlen = 0;
+             fd = BADFD == -1 ? newfd : BADFD;
+           }
          else
            {
          else
            {
-             /* Remove any old directory info, and add new info.  */
-             if (0 < c->subdirlen && !chdirable (fd))
-               close (fd);
              c->chdir_current = chdir_current;
              c->fd = fd = newfd;
              c->subdirlen = subdirlen;
              c->chdir_current = chdir_current;
              c->fd = fd = newfd;
              c->subdirlen = subdirlen;
index 6583ba1b6c276abd20b0c8cdb8ce87ddf1d46817..b68fd83ce9da8e1d11168ece41b5b90151a1951e 100644 (file)
@@ -144,6 +144,7 @@ TESTSUITE_AT = \
  extrac32.at\
  extrac33.at\
  extrac34.at\
  extrac32.at\
  extrac33.at\
  extrac34.at\
+ extrac35.at\
  filerem01.at\
  filerem02.at\
  filerem03.at\
  filerem01.at\
  filerem02.at\
  filerem03.at\
diff --git a/tests/extrac35.at b/tests/extrac35.at
new file mode 100644 (file)
index 0000000..2a536af
--- /dev/null
@@ -0,0 +1,37 @@
+# Check failed parent lookup does not corrupt the directory cache. -*- Autotest -*-
+
+# Copyright 2026 Free Software Foundation, Inc.
+
+# This file is part of GNU tar.
+
+# GNU tar is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+
+# GNU tar is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+AT_SETUP([failed parent lookup and directory cache])
+AT_KEYWORDS([extract extrac35 chdir])
+
+AT_TAR_CHECK([
+mkdir -p src/a src/b dest
+printf 'one\n' > src/a/one
+printf 'two\n' > src/b/two
+
+tar -cf archive.tar -C src --no-recursion a/one b/two &&
+tar -xf archive.tar -C dest &&
+test -f dest/a/one &&
+test -f dest/b/two &&
+test ! -e dest/a/two &&
+cmp src/a/one dest/a/one &&
+cmp src/b/two dest/b/two
+])
+
+AT_CLEANUP
index 9eb0d11c43c406ef4d2f6858b2e690608272f9cb..29982cd1dd7549de7c384e2f12eafd8846ebace5 100644 (file)
@@ -361,6 +361,7 @@ m4_include([extrac31.at])
 m4_include([extrac32.at])
 m4_include([extrac33.at])
 m4_include([extrac34.at])
 m4_include([extrac32.at])
 m4_include([extrac33.at])
 m4_include([extrac34.at])
+m4_include([extrac35.at])
 
 m4_include([backup01.at])
 
 
 m4_include([backup01.at])