From 0518e33f6c0c3a9f6ce1f800ca4b7fe5f3a1b7a5 Mon Sep 17 00:00:00 2001 From: matsuyoshi30 Date: Thu, 19 Jan 2023 00:45:18 +0900 Subject: [PATCH] path/filepath: fix evaluation of symlinks to paths under /tmp on macOS For symlinks created from symlinks under the root directory created as the relative path (e.g., symbolic links under /tmp), we update vol and volLen. Fixes #57905 Change-Id: I45affd1db3b93109de51bf19b181f3cdba061109 Reviewed-on: https://go-review.googlesource.com/c/go/+/461761 Run-TryBot: Bryan Mills TryBot-Result: Gopher Robot Reviewed-by: Bryan Mills Auto-Submit: Bryan Mills Reviewed-by: Ian Lance Taylor --- src/path/filepath/path_test.go | 31 +++++++++++++++++++++++++++++++ src/path/filepath/symlink.go | 2 ++ 2 files changed, 33 insertions(+) diff --git a/src/path/filepath/path_test.go b/src/path/filepath/path_test.go index 6647444852..e6a9270909 100644 --- a/src/path/filepath/path_test.go +++ b/src/path/filepath/path_test.go @@ -1193,6 +1193,37 @@ func TestIssue13582(t *testing.T) { } } +// Issue 57905. +func TestRelativeSymlinkToAbsolute(t *testing.T) { + testenv.MustHaveSymlink(t) + // Not parallel: uses os.Chdir. + + tmpDir := t.TempDir() + chdir(t, tmpDir) + + // Create "link" in the current working directory as a symlink to an arbitrary + // absolute path. On macOS, this path is likely to begin with a symlink + // itself: generally either in /var (symlinked to "private/var") or /tmp + // (symlinked to "private/tmp"). + if err := os.Symlink(tmpDir, "link"); err != nil { + t.Fatal(err) + } + t.Logf(`os.Symlink(%q, "link")`, tmpDir) + + p, err := filepath.EvalSymlinks("link") + if err != nil { + t.Fatalf(`EvalSymlinks("link"): %v`, err) + } + want, err := filepath.EvalSymlinks(tmpDir) + if err != nil { + t.Fatalf(`EvalSymlinks(%q): %v`, tmpDir, err) + } + if p != want { + t.Errorf(`EvalSymlinks("link") = %q; want %q`, p, want) + } + t.Logf(`EvalSymlinks("link") = %q`, p) +} + // Test directories relative to temporary directory. // The tests are run in absTestDirs[0]. var absTestDirs = []string{ diff --git a/src/path/filepath/symlink.go b/src/path/filepath/symlink.go index 6fefd15977..f9435e0d5b 100644 --- a/src/path/filepath/symlink.go +++ b/src/path/filepath/symlink.go @@ -126,6 +126,8 @@ func walkSymlinks(path string) (string, error) { // Symlink to absolute path. dest = link[:1] end = 1 + vol = link[:1] + volLen = 1 } else { // Symlink to relative path; replace last // path component in dest. -- 2.50.0