* src/ln.c (do_link): Don't set SOURCE to REL_SOURCE before the backup
checks.
* tests/ln/relative.sh: Add two test cases.
* NEWS: Mention the bug fix.

Discovered by Anthropic's OSS Scanner, as bug ANT-2026-ET9AU19B.
---
 NEWS                 |  6 ++++++
 src/ln.c             |  9 ++++++++-
 tests/ln/relative.sh | 20 ++++++++++++++++++++
 3 files changed, 34 insertions(+), 1 deletion(-)

diff --git a/NEWS b/NEWS
index 840dfddb5..f6e1662a4 100644
--- a/NEWS
+++ b/NEWS
@@ -156,6 +156,12 @@ GNU coreutils NEWS                                    -*- 
outline -*-
   on systems providing support for atomic rename.
   [This bug was present in "the beginning".]
 
+  'ln -srf' no longer replaces files with a symbolic link to itself when the
+  target and link are the same and reside in a directory that is not the 
current
+  working directory.  E.g., 'ln -srf d/f d/f' now fails like 'ln -sf d/f d/f'
+  does.
+  [bug introduced in coreutils-8.31]
+
   'ls' no longer omits ACL indicators or capability coloring for subsequent
   files on the same file system after an unsupported security context
   lookup on a file without an ACL.
diff --git a/src/ln.c b/src/ln.c
index 2f0c2e112..a394172b6 100644
--- a/src/ln.c
+++ b/src/ln.c
@@ -207,8 +207,9 @@ do_link (char const *source, int destdir_fd, char const 
*dest_base,
           return false;
         }
 
+      /* Determine REL_SOURCE before any backup of DEST.  */
       if (relative)
-        source = rel_source = convert_abs_rel (source, dest);
+        rel_source = convert_abs_rel (source, dest);
 
       bool force = (remove_existing_files || interactive
                     || backup_type != no_backups);
@@ -302,6 +303,12 @@ do_link (char const *source, int destdir_fd, char const 
*dest_base,
             }
         }
 
+      /* The above backup checks interpret SOURCE relative to the current
+         working directory.  Now that they are done, we can update SOURCE
+         to REL_SOURCE.  */
+      if (rel_source)
+        source = rel_source;
+
       /* If the attempt to create a link fails and we are removing or
          backing up destinations, unlink the destination and try again.
 
diff --git a/tests/ln/relative.sh b/tests/ln/relative.sh
index e4bd9d7b1..66d352a33 100755
--- a/tests/ln/relative.sh
+++ b/tests/ln/relative.sh
@@ -45,6 +45,26 @@ mkdir web
 ln -sr latest web/latest
 test $(readlink web/latest) = '../release2' || fail=1
 
+# This would replace d/a with a symbolic link to itself with coreutils-8.31
+# through coreutils-9.12.
+echo a >exp-a &&
+mkdir d &&
+cp exp-a d/a &&
+echo "ln: 'd/a' and 'd/a' are the same file" >exp-err || framework_failure_
+returns_ 1 ln -srf d/a d/a >out 2>err || fail=1
+compare /dev/null out || fail=1
+compare exp-err err || fail=1
+compare exp-a d/a || fail=1
+
+# Likewise, using a symbolic link target directory.
+ln -s d d-slink &&
+echo "ln: 'd/a' and 'd-slink/a' are the same file" >exp-err \
+  || framework_failure_
+returns_ 1 ln -srf d/a d-slink >out 2>err || fail=1
+compare /dev/null out || fail=1
+compare exp-err err || fail=1
+compare exp-a d/a || fail=1
+
 # Expect this to fail with exit status 1, or to succeed quietly (freebsd).
 # Prior to coreutils-8.23, it would segfault.
 ln -sr '' F
-- 
2.56.1


Reply via email to