Skip to content

Commit a704d39

Browse files
mknyszekgopherbot
authored andcommitted
os: hide SetFinalizer from users of Root
Currently Root embeds a root and calls SetFinalizer on &r.root. This sets the finalizer on the outer root, which is visible to users of os.Root, and thus they can mutate the finalizer attached to it. This change modifies Root to not embed its inner root, but rather to refer to it by pointer. This allows us to set the finalizer on this independent inner object, preventing users of os.Root from changing the finalizer. This follows the same pattern as os.File's finalizer. Fixes #71617. Change-Id: Ibd199bab1b3c877d5e12ef380fd4647b4e10221f Reviewed-on: https://go-review.googlesource.com/c/go/+/647876 Auto-Submit: Michael Knyszek <[email protected]> Reviewed-by: Damien Neil <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 47d0b0f commit a704d39

File tree

4 files changed

+6
-6
lines changed

4 files changed

+6
-6
lines changed

src/os/root.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func OpenInRoot(dir, name string) (*File, error) {
6060
// - When GOOS=plan9 or GOOS=js, Root does not track directories across renames.
6161
// On these platforms, a Root references a directory name, not a file descriptor.
6262
type Root struct {
63-
root root
63+
root *root
6464
}
6565

6666
const (

src/os/root_noopenat.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func newRoot(name string) (*Root, error) {
4949
if !fi.IsDir() {
5050
return nil, errors.New("not a directory")
5151
}
52-
return &Root{root{name: name}}, nil
52+
return &Root{&root{name: name}}, nil
5353
}
5454

5555
func (r *root) Close() error {

src/os/root_unix.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@ func newRoot(fd int, name string) (*Root, error) {
4848
syscall.CloseOnExec(fd)
4949
}
5050

51-
r := &Root{root{
51+
r := &Root{&root{
5252
fd: fd,
5353
name: name,
5454
}}
55-
runtime.SetFinalizer(&r.root, (*root).Close)
55+
runtime.SetFinalizer(r.root, (*root).Close)
5656
return r, nil
5757
}
5858

src/os/root_windows.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,11 @@ func newRoot(fd syscall.Handle, name string) (*Root, error) {
105105
return nil, &PathError{Op: "open", Path: name, Err: errors.New("not a directory")}
106106
}
107107

108-
r := &Root{root{
108+
r := &Root{&root{
109109
fd: fd,
110110
name: name,
111111
}}
112-
runtime.SetFinalizer(&r.root, (*root).Close)
112+
runtime.SetFinalizer(r.root, (*root).Close)
113113
return r, nil
114114
}
115115

0 commit comments

Comments
 (0)