Skip to content

Commit

Permalink
Merge pull request goplus#828 from luoliwoshang/os/errno
Browse files Browse the repository at this point in the history
os:fix os.Errno 's nil pointer derefer in linux
  • Loading branch information
xushiwei authored Oct 21, 2024
2 parents aa560f4 + e9177c8 commit 1b3bb86
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 22 deletions.
28 changes: 28 additions & 0 deletions _demo/checkfile/demo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package main

import (
"fmt"
"os"
"path/filepath"
)

func main() {
tempDir := os.TempDir()
noexist := filepath.Join(tempDir, "noexist.txt")

if _, err := os.Stat(noexist); err != nil {
if os.IsNotExist(err) {
fmt.Println("noexist:", err.Error())
} else {
fmt.Println("exist,other err:", err.Error())
}
}

if _, err := os.Open(noexist); err != nil {
if os.IsNotExist(err) {
fmt.Println("noexist:", err.Error())
} else {
fmt.Println("exist,other err:", err.Error())
}
}
}
3 changes: 3 additions & 0 deletions c/os/_os/os.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <stdlib.h>
#include <errno.h>

int llgoClearenv() {
extern char **environ;
Expand All @@ -7,3 +8,5 @@ int llgoClearenv() {
}
return 0;
}

int llgoErrno() { return errno; }
4 changes: 2 additions & 2 deletions c/os/os.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ type (
StatT = syscall.Stat_t
)

//go:linkname Errno errno
var Errno c.Int
//go:linkname Errno C.llgoErrno
func Errno() c.Int

//go:linkname Umask C.umask
func Umask(cmask ModeT) ModeT
Expand Down
3 changes: 2 additions & 1 deletion c/os/os_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ package os
import "C"

const (
LLGoPackage = "decl"
LLGoFiles = "_os/os.c"
LLGoPackage = "link"
)

//go:linkname Clearenv C.clearenv
Expand Down
6 changes: 3 additions & 3 deletions internal/build/_overlay/net/textproto/textproto.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func (conn *cConn) Read(p []byte) (n int, err error) {
for n < len(p) {
result := os.Read(conn.socketFd, unsafe.Pointer(&p[n:][0]), uintptr(len(p)-n))
if result < 0 {
if os.Errno == c.Int(syscall.EINTR) {
if os.Errno() == c.Int(syscall.EINTR) {
continue
}
return n, errors.New("read error")
Expand All @@ -128,7 +128,7 @@ func (conn *cConn) Write(p []byte) (n int, err error) {
for n < len(p) {
result := os.Write(conn.socketFd, unsafe.Pointer(&p[n:][0]), uintptr(len(p)-n))
if result < 0 {
if os.Errno == c.Int(syscall.EINTR) {
if os.Errno() == c.Int(syscall.EINTR) {
continue
}
return n, errors.New("write error")
Expand All @@ -151,7 +151,7 @@ func (conn *cConn) Close() error {
conn.closed = true
result := os.Close(conn.socketFd)
if result < 0 {
return errors.New(c.GoString(c.Strerror(os.Errno)))
return errors.New(c.GoString(c.Strerror(os.Errno())))
}
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion internal/lib/os/os.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ func Getwd() (dir string, err error) {
if wd != nil {
return c.GoString(wd), nil
}
return "", syscall.Errno(os.Errno)
return "", syscall.Errno(os.Errno())
}

// TODO(xsw):
Expand Down
4 changes: 2 additions & 2 deletions internal/lib/os/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (f *File) write(b []byte) (int, error) {
if ret >= 0 {
return int(ret), nil
}
return 0, syscall.Errno(os.Errno)
return 0, syscall.Errno(os.Errno())
}

/* TODO(xsw):
Expand All @@ -56,7 +56,7 @@ func (f *File) read(b []byte) (int, error) {
if ret == 0 {
return 0, io.EOF
}
return 0, syscall.Errno(os.Errno)
return 0, syscall.Errno(os.Errno())
}

/* TODO(xsw):
Expand Down
4 changes: 2 additions & 2 deletions internal/lib/syscall/exec_libc2.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,14 +264,14 @@ func forkAndExecInChild(argv0 *c.Char, argv, envv **c.Char, chroot, dir *c.Char,
// dup2(i, i) won't clear close-on-exec flag on Linux,
// probably not elsewhere either.
if ret := os.Fcntl(c.Int(fd[i]), syscall.F_SETFD, 0); ret < 0 {
err1 = Errno(os.Errno)
err1 = Errno(os.Errno())
goto childerror
}
continue
}
// The new fd is created NOT close-on-exec,
if ret := os.Dup2(c.Int(fd[i]), c.Int(i)); ret < 0 {
err1 = Errno(os.Errno)
err1 = Errno(os.Errno())
goto childerror
}
}
Expand Down
20 changes: 10 additions & 10 deletions internal/lib/syscall/syscall.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,15 @@ func Getcwd(buf []byte) (n int, err error) {
if ret != nil {
return int(c.Strlen(ret)), nil
}
return 0, Errno(os.Errno)
return 0, Errno(os.Errno())
}

func Getwd() (string, error) {
wd := os.Getcwd(c.Alloca(os.PATH_MAX), os.PATH_MAX)
if wd != nil {
return c.GoString(wd), nil
}
return "", Errno(os.Errno)
return "", Errno(os.Errno())
}

func Getpid() (pid int) {
Expand All @@ -90,47 +90,47 @@ func fork() (uintptr, Errno) {
if ret >= 0 {
return uintptr(ret), Errno(0)
}
return 0, Errno(os.Errno)
return 0, Errno(os.Errno())
}

func wait4(pid int, wstatus *c.Int, options int, rusage *syscall.Rusage) (wpid int, err error) {
ret := os.Wait4(os.PidT(pid), wstatus, c.Int(options), rusage)
if ret >= 0 {
return int(ret), nil
}
return 0, Errno(os.Errno)
return 0, Errno(os.Errno())
}

func Open(path string, mode int, perm uint32) (fd int, err error) {
ret := os.Open(c.AllocaCStr(path), c.Int(mode), os.ModeT(perm))
if ret >= 0 {
return int(ret), nil
}
return 0, Errno(os.Errno)
return 0, Errno(os.Errno())
}

func Seek(fd int, offset int64, whence int) (newoffset int64, err error) {
ret := os.Lseek(c.Int(fd), os.OffT(offset), c.Int(whence))
if ret >= 0 {
return int64(ret), nil
}
return -1, Errno(os.Errno)
return -1, Errno(os.Errno())
}

func Read(fd int, p []byte) (n int, err error) {
ret := os.Read(c.Int(fd), unsafe.Pointer(unsafe.SliceData(p)), uintptr(len(p)))
if ret >= 0 {
return ret, nil // TODO(xsw): confirm err == nil (not io.EOF) when ret == 0
}
return 0, Errno(os.Errno)
return 0, Errno(os.Errno())
}

func readlen(fd int, buf *byte, nbuf int) (n int, err error) {
ret := os.Read(c.Int(fd), unsafe.Pointer(buf), uintptr(nbuf))
if ret >= 0 {
return ret, nil // TODO(xsw): confirm err == nil (not io.EOF) when ret == 0
}
return 0, Errno(os.Errno)
return 0, Errno(os.Errno())
}

func Close(fd int) (err error) {
Expand All @@ -148,15 +148,15 @@ func Lstat(path string, stat *Stat_t) (err error) {
if ret == 0 {
return nil
}
return Errno(os.Errno)
return Errno(os.Errno())
}

func Stat(path string, stat *Stat_t) (err error) {
ret := os.Stat(c.AllocaCStr(path), stat)
if ret == 0 {
return nil
}
return Errno(os.Errno)
return Errno(os.Errno())
}

func Pipe(p []int) (err error) {
Expand Down
2 changes: 1 addition & 1 deletion internal/lib/syscall/syscall_bsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ func SysctlUint32(name string) (value uint32, err error) {
n := uintptr(4)
ret := os.Sysctlbyname(c.AllocaCStr(name), unsafe.Pointer(&value), &n, nil, 0)
if ret != 0 {
err = Errno(os.Errno)
err = Errno(os.Errno())
}
return
}
Expand Down

0 comments on commit 1b3bb86

Please sign in to comment.