You can repro this with:

  $ mkdir d
  $ touch src d/file d/file.~1~
  $ chmod a-r d
  $ timeout 1 cp --backup=numbered src d/file || echo would hang

* lib/backupfile.c (backupfile_internal): Don't retry
with the same guess if we weren't able to open the directory.
---
 ChangeLog        | 6 ++++++
 lib/backupfile.c | 2 +-
 2 files changed, 7 insertions(+), 1 deletion(-)

diff --git a/ChangeLog b/ChangeLog
index 88c7ce950a..81fca3fd5a 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2026-11-15  Pádraig Brady  <[email protected]>
+
+       backupfile: fix infinite loop with unreadable directories
+       * lib/backupfile.c (backupfile_internal): Don't retry
+       with the same guess if we weren't able to open the directory.
+
 2026-11-14  Pádraig Brady  <[email protected]>
 
        fchmodat: require AT_SYMLINK_NOFOLLOW emulation on glibc 2.32-2.38
diff --git a/lib/backupfile.c b/lib/backupfile.c
index dcd4b0bed4..d25102b02c 100644
--- a/lib/backupfile.c
+++ b/lib/backupfile.c
@@ -377,7 +377,7 @@ backupfile_internal (int dir_fd, char const *file,
       if (renameatu (dir_fd, file + offset, dir_fd, s + offset, flags) == 0)
         break;
       int saved_errno = errno;
-      if (! (saved_errno == EEXIST && extended))
+      if (! (saved_errno == EEXIST && extended && dirp))
         {
           if (dirp)
             closedir (dirp);
-- 
2.56.0


Reply via email to