tizen: Fix ln --relative 77/309577/2 accepted/tizen_9.0_base accepted/tizen_base accepted/tizen_base_asan accepted/tizen_base_dev accepted/tizen_base_toolchain accepted/tizen_base_x accepted/tizen_base_x_asan tizen_9.0_base tizen_base accepted/tizen/9.0/base/20241030.075456 accepted/tizen/base/20240612.043335 accepted/tizen/base/asan/20240806.090134 accepted/tizen/base/dev/20241206.041254 accepted/tizen/base/toolchain/20240624.003656 accepted/tizen/base/x/20240612.051126 accepted/tizen/base/x/asan/20240624.231535 tizen_9.0_m2_release
authorŁukasz Stelmach <l.stelmach@samsung.com>
Tue, 9 Apr 2024 14:14:21 +0000 (16:14 +0200)
committerŁukasz Stelmach <l.stelmach@samsung.com>
Wed, 29 May 2024 22:29:32 +0000 (00:29 +0200)
Include the a trailing slash when comparing directory names with
strncmp(3).

Add more thorough tests.

Change-Id: I269988d1513db8b61f72be20ccad5ff1ca0f33ea
Signed-off-by: Łukasz Stelmach <l.stelmach@samsung.com>
src/ln.c
tests/ln/relative

index 97c32683e88d219bebcfa3530d513c2e3d4d98e4..8db2047342f9bf28e7498e244d599089087fc233 100644 (file)
--- a/src/ln.c
+++ b/src/ln.c
@@ -274,7 +274,7 @@ do_link (const char *source, const char *dest)
           d = canon_dest = canonicalize_filename_mode(dest, CAN_ALL_BUT_LAST);
           if (!d)
             {
-              error(0, errno, "cannot canonicalize destination file name: %s", quote(source));
+              error(0, errno, "cannot canonicalize destination file name: %s", quote(dest));
               free(canon_source);
               return false;
             }
@@ -292,7 +292,7 @@ do_link (const char *source, const char *dest)
           s++; d++;
           sp = strchr(s, '/');
           dp = strchr(d, '/');
-          while (strncmp(s, d, sp - s) == 0)
+          while (strncmp(s, d, sp - s + 1) == 0)
             {
               d = dp + 1;
               s = sp + 1;
@@ -304,7 +304,7 @@ do_link (const char *source, const char *dest)
           memset(relative_source, 0, PATH_MAX);
           relative_len = 0;
 
-          while ((relative_len < (PATH_MAX - 1 - 3)) && (strncmp(d, dest_base, dp - d) != 0))
+          while ((relative_len < (PATH_MAX - 1 - 3)) && (strncmp(d, dest_base, dp - d + 1) != 0))
             {
               d = dp + 1;
               dp = strchr(d , '/');
index 423153168794cca5487b732a7dcaf605b67d5cca..e4ad4625a545fba308719b77650bdcbe57e1b38f 100755 (executable)
@@ -29,10 +29,11 @@ trap 'status=$?; cd "$pwd" && chmod -R u+rwx $t0 && rm -rf $t0 && exit $status'
 trap '(exit $?); exit $?' 1 2 13 15
 
 framework_failure=0
-mkdir -p $tmp/a || framework_failure=1
-mkdir -p $tmp/z || framework_failure=1
-touch $tmp/b || framework_failure=1
-touch $tmp/a/b || framework_failure=1
+mkdir -p $tmp/A/B || framework_failure=1
+mkdir -p $tmp/Z/Y || framework_failure=1
+touch $tmp/a || framework_failure=1
+touch $tmp/A/b || framework_failure=1
+touch $tmp/A/B/c || framework_failure=1
 cd $tmp || framework_failure=1
 
 if test $framework_failure = 1; then
@@ -42,16 +43,32 @@ fi
 
 fail=0
 
-ln -sr ./a/b ./y
-test $(readlink y) = 'a/b' || fail=1
+ln -srT ./a     ./x
+test $(readlink x) = 'a' || fail=1
 
-ln -sr ./a/b ./z/y
-test $(readlink z/y) = '../a/b' || fail=1
+ln -srT ./A/b   ./y
+test $(readlink y) = 'A/b' || fail=1
 
-ln -sr ./b ./z/x
-test $(readlink z/x) = '../b' || fail=1
+ln -srT ./A/B/c ./z
+test $(readlink z) = 'A/B/c' || fail=1
+
+ln -srT ./a     ./Z/x
+test $(readlink Z/x) = '../a' || fail=1
+
+ln -srT ./A/b   ./Z/y
+test $(readlink Z/y) = '../A/b' || fail=1
+
+ln -srT ./A/B/c ./Z/z
+test $(readlink Z/z) = '../A/B/c' || fail=1
+
+ln -srT $(pwd)/a     $(pwd)/Z/Y/x
+test $(readlink Z/Y/x) = '../../a' || fail=1
+
+ln -srT ./A/b   ./Z/Y/Y
+test $(readlink Z/Y/Y) = '../../A/b' || fail=1
+
+ln -srT ./A/B/c ./Z/Y/z
+test $(readlink Z/Y/z) = '../../A/B/c' || fail=1
 
-ln -sr ./b ./x
-test $(readlink x) = 'b' || fail=1
 
 (exit $fail); exit $fail