* src/ln.c (convert_abs_rel): Fallback either to the absolute target,
the specified absolute target, or indicate failure.
(do_link): Diagnose a convert_abs_rel() failure.
Also avoid calling covert_abs_rel() for an empty string.
* tests/ln/relative.sh: Add a test case.
* NEWS: Mention the bug fix.

Discovered by Anthropic's OSS Scanner, as bug ANT-2026-9XWA0JNW.
---
 NEWS                 |  5 +++++
 src/ln.c             | 15 +++++++++++++--
 tests/ln/relative.sh | 21 +++++++++++++++++----
 3 files changed, 35 insertions(+), 6 deletions(-)

diff --git a/NEWS b/NEWS
index d03316785..ba8beb685 100644
--- a/NEWS
+++ b/NEWS
@@ -167,6 +167,11 @@ GNU coreutils NEWS                                    -*- 
outline -*-
   on systems providing support for atomic rename.
   [This bug was present in "the beginning".]
 
+  'ln -sr' with a relative target operand now uses an absolute path or reports
+  failure if it cannot generate a path relative to the link's directory.
+  Previously it could create a link pointing to the wrong file.
+  [bug introduced with the --relative feature in coreutils-8.16]
+
   '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'
diff --git a/src/ln.c b/src/ln.c
index a394172b6..08c0369a0 100644
--- a/src/ln.c
+++ b/src/ln.c
@@ -118,6 +118,7 @@ errnoize (int status)
 }
 
 /* Return FROM represented as relative to the dir of TARGET.
+   Fall back to an absolute name, or return NULL with errno set.
    The result is malloced.  */
 
 static char *
@@ -145,6 +146,8 @@ convert_abs_rel (char const *from, char const *target)
 
   free (targetdir);
   free (realdest);
+  if (!relative_from && !IS_ABSOLUTE_FILE_NAME (from))
+    return realfrom;
   free (realfrom);
 
   return relative_from ? relative_from : xstrdup (from);
@@ -208,8 +211,16 @@ do_link (char const *source, int destdir_fd, char const 
*dest_base,
         }
 
       /* Determine REL_SOURCE before any backup of DEST.  */
-      if (relative)
-        rel_source = convert_abs_rel (source, dest);
+      if (relative && *source)
+        {
+          rel_source = convert_abs_rel (source, dest);
+          if (!rel_source)
+            {
+              error (0, errno, _("failed to create symbolic link %s -> %s"),
+                     quoteaf_n (0, dest), quoteaf_n (1, source));
+              return false;
+            }
+        }
 
       bool force = (remove_existing_files || interactive
                     || backup_type != no_backups);
diff --git a/tests/ln/relative.sh b/tests/ln/relative.sh
index 66d352a33..b8b52b425 100755
--- a/tests/ln/relative.sh
+++ b/tests/ln/relative.sh
@@ -65,9 +65,22 @@ 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
-case $? in [01]) ;; *) fail=1;; esac
+# A relative operand that cannot be canonicalized must not be installed
+# verbatim in another directory.  An absolute operand can be kept as given.
+ln -s cycle/x cycle || framework_failure_
+returns_ 1 ln -srf cycle d/a || fail=1
+compare exp-a d/a || fail=1
+abs_cycle=$(pwd)/./cycle
+ln -sr "$abs_cycle" web/cycle || fail=1
+test "$(readlink web/cycle)" = "$abs_cycle" || fail=1
+
+# Preserve an empty target on systems that accept one (e.g., FreeBSD).
+# Prior to coreutils-8.23, this would segfault.
+if ln -s '' plain 2>/dev/null; then
+  ln -sr '' F || fail=1
+  test -L F || fail=1
+else
+  returns_ 1 ln -sr '' F || fail=1
+fi
 
 Exit $fail
-- 
2.56.0


Reply via email to