Skip to content
Snippets Groups Projects
Commit fa7217f7 authored by Damien Neil's avatar Damien Neil Committed by Carlos Amedee
Browse files

[release-branch.go1.24] os: avoid panic in Root when symlink references the root

We would panic when opening a symlink ending in ..,
where the symlink references the root itself.

For #73081
Fixes #73082

Change-Id: I7dc3f041ca79df7942feec58c197fde6881ecae5
Reviewed-on: https://go-review.googlesource.com/c/go/+/661416


Reviewed-by: default avatarAlan Donovan <adonovan@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
(cherry picked from commit cfc784a1)
Reviewed-on: https://go-review.googlesource.com/c/go/+/662315
parent 49860cf9
No related branches found
No related tags found
No related merge requests found
......@@ -146,6 +146,9 @@ func doInRoot[T any](r *Root, name string, f func(parent sysfdType, name string)
return ret, errPathEscapes
}
parts = slices.Delete(parts, i-count, end)
if len(parts) == 0 {
parts = []string{"."}
}
i = 0
if dirfd != rootfd {
syscall.Close(dirfd)
......
......@@ -1176,6 +1176,33 @@ func TestRootRaceRenameDir(t *testing.T) {
}
}
func TestRootSymlinkToRoot(t *testing.T) {
dir := makefs(t, []string{
"d/d => ..",
})
root, err := os.OpenRoot(dir)
if err != nil {
t.Fatal(err)
}
defer root.Close()
if err := root.Mkdir("d/d/new", 0777); err != nil {
t.Fatal(err)
}
f, err := root.Open("d/d")
if err != nil {
t.Fatal(err)
}
defer f.Close()
names, err := f.Readdirnames(-1)
if err != nil {
t.Fatal(err)
}
slices.Sort(names)
if got, want := names, []string{"d", "new"}; !slices.Equal(got, want) {
t.Errorf("root contains: %q, want %q", got, want)
}
}
func TestOpenInRoot(t *testing.T) {
dir := makefs(t, []string{
"file",
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment