Skip to content

Commit a680bfb

Browse files
QuLogicaykevl
authored andcommitted
os: Use a uintptr for NewFile
This appears to have been how it is upstream for about 10 years. Using an interface breaks if a file descriptor number is passed directly to `NewFile`, e.g., `NewFile(3, "fuzz_in")` as used in Go 1.18 fuzzing code.
1 parent bf23839 commit a680bfb

File tree

5 files changed

+19
-25
lines changed

5 files changed

+19
-25
lines changed

src/os/file_anyos.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ func init() {
2121
// Stdin, Stdout, and Stderr are open Files pointing to the standard input,
2222
// standard output, and standard error file descriptors.
2323
var (
24-
Stdin = NewFile(unixFileHandle(syscall.Stdin), "/dev/stdin")
25-
Stdout = NewFile(unixFileHandle(syscall.Stdout), "/dev/stdout")
26-
Stderr = NewFile(unixFileHandle(syscall.Stderr), "/dev/stderr")
24+
Stdin = NewFile(uintptr(syscall.Stdin), "/dev/stdin")
25+
Stdout = NewFile(uintptr(syscall.Stdout), "/dev/stdout")
26+
Stderr = NewFile(uintptr(syscall.Stderr), "/dev/stderr")
2727
)
2828

2929
const DevNull = "/dev/null"
@@ -87,9 +87,9 @@ func (fs unixFilesystem) Remove(path string) error {
8787
return &PathError{Op: "remove", Path: path, Err: e}
8888
}
8989

90-
func (fs unixFilesystem) OpenFile(path string, flag int, perm FileMode) (FileHandle, error) {
90+
func (fs unixFilesystem) OpenFile(path string, flag int, perm FileMode) (uintptr, error) {
9191
fp, err := syscall.Open(path, flag, uint32(perm))
92-
return unixFileHandle(fp), handleSyscallError(err)
92+
return uintptr(fp), handleSyscallError(err)
9393
}
9494

9595
// unixFileHandle is a Unix file pointer with associated methods that implement

src/os/file_other.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ import (
1010
// Stdin, Stdout, and Stderr are open Files pointing to the standard input,
1111
// standard output, and standard error file descriptors.
1212
var (
13-
Stdin = NewFile(stdioFileHandle(0), "/dev/stdin")
14-
Stdout = NewFile(stdioFileHandle(1), "/dev/stdout")
15-
Stderr = NewFile(stdioFileHandle(2), "/dev/stderr")
13+
Stdin = NewFile(0, "/dev/stdin")
14+
Stdout = NewFile(1, "/dev/stdout")
15+
Stderr = NewFile(2, "/dev/stderr")
1616
)
1717

1818
// isOS indicates whether we're running on a real operating system with
@@ -32,8 +32,8 @@ type file struct {
3232
name string
3333
}
3434

35-
func NewFile(fd FileHandle, name string) *File {
36-
return &File{&file{fd, name}}
35+
func NewFile(fd uintptr, name string) *File {
36+
return &File{&file{stdioFileHandle(fd), name}}
3737
}
3838

3939
// Read is unsupported on this system.

src/os/file_unix.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ type file struct {
3838
dirinfo *dirInfo // nil unless directory being read
3939
}
4040

41-
func NewFile(fd FileHandle, name string) *File {
42-
return &File{&file{fd, name, nil}}
41+
func NewFile(fd uintptr, name string) *File {
42+
return &File{&file{unixFileHandle(fd), name, nil}}
4343
}
4444

4545
func Pipe() (r *File, w *File, err error) {
@@ -48,8 +48,8 @@ func Pipe() (r *File, w *File, err error) {
4848
if err != nil {
4949
return
5050
}
51-
r = NewFile(unixFileHandle(p[0]), "|0")
52-
w = NewFile(unixFileHandle(p[1]), "|1")
51+
r = NewFile(uintptr(p[0]), "|0")
52+
w = NewFile(uintptr(p[1]), "|1")
5353
return
5454
}
5555

src/os/file_windows.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ type file struct {
3838
name string
3939
}
4040

41-
func NewFile(fd FileHandle, name string) *File {
42-
return &File{&file{fd, name}}
41+
func NewFile(fd uintptr, name string) *File {
42+
return &File{&file{unixFileHandle(fd), name}}
4343
}
4444

4545
func Pipe() (r *File, w *File, err error) {
@@ -48,14 +48,8 @@ func Pipe() (r *File, w *File, err error) {
4848
if e != nil {
4949
return nil, nil, err
5050
}
51-
r = NewFile(
52-
unixFileHandle(p[0]),
53-
"|0",
54-
)
55-
w = NewFile(
56-
unixFileHandle(p[1]),
57-
"|1",
58-
)
51+
r = NewFile(uintptr(p[0]), "|0")
52+
w = NewFile(uintptr(p[1]), "|1")
5953
return
6054
}
6155

src/os/filesystem.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ type mountPoint struct {
2828
// WARNING: this interface is not finalized and may change in a future version.
2929
type Filesystem interface {
3030
// OpenFile opens the named file.
31-
OpenFile(name string, flag int, perm FileMode) (FileHandle, error)
31+
OpenFile(name string, flag int, perm FileMode) (uintptr, error)
3232

3333
// Mkdir creates a new directoy with the specified permission (before
3434
// umask). Some filesystems may not support directories or permissions.

0 commit comments

Comments
 (0)