Should I add a news entry for this one? I imagine the error is rarely
seen since, in practice, it only shows up when you are root and
fchownat fails, which should be rare.

I was tempted to skip adding a test for it, but I figured making
excuses not to write tests is a bad habit. :)

-- 8< --

* src/copy.c (copy_internal): Add a missing quoteaf call.
* tests/cp/link-no-deref.sh: Invoke getlimits_ and uses_strace_. Inject
an error using 'strace' to verify the name of the destination is quoted
in the error message.

Discovered by Anthropic's OSS Scanner, as bug ANT-2026-3M3N6L8K.
---
 src/copy.c                |  2 +-
 tests/cp/link-no-deref.sh | 17 +++++++++++++++++
 2 files changed, 18 insertions(+), 1 deletion(-)

diff --git a/src/copy.c b/src/copy.c
index aedbd0dc3..7964a6eeb 100644
--- a/src/copy.c
+++ b/src/copy.c
@@ -2569,7 +2569,7 @@ skip:
               && ! chown_failure_ok (x))
             {
               error (0, errno, _("failed to preserve ownership for %s"),
-                     dst_name);
+                     quoteaf (dst_name));
               if (x->require_preserve)
                 goto un_backup;
             }
diff --git a/tests/cp/link-no-deref.sh b/tests/cp/link-no-deref.sh
index 92d4d36b5..c21764355 100755
--- a/tests/cp/link-no-deref.sh
+++ b/tests/cp/link-no-deref.sh
@@ -18,6 +18,8 @@
 
 . "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
 print_ver_ cp
+getlimits_
+uses_strace_
 
 ln -s no-such-file dangling-slink || framework_failure_
 
@@ -26,4 +28,19 @@ ln -s no-such-file dangling-slink || framework_failure_
 # with link being applied to the dangling symlink.
 cp --link --no-dereference dangling-slink d2 || fail=1
 
+NL='
+'
+chowns=chown,lchown,fchownat
+if strace --quiet=all -o /dev/null -P foo \
+          -e inject=$chowns:error=EPERM true 2>/dev/null; then
+  echo "cp: failed to preserve ownership for ''\$'\\n': $ENOSYS" >exp \
+    || framework_failure_
+  touch file && ln -s file slink || framework_failure_
+  returns_ 1 strace --quiet=all -o /dev/null -P "$NL" \
+    -e inject=$chowns:error=ENOSYS cp -P --preserve=ownership slink "$NL" \
+    >out 2>err || fail=1
+  compare exp err || fail=1
+  compare /dev/null out || fail=1
+fi
+
 Exit $fail
-- 
2.56.0


Reply via email to