Skip to content

not able to properly check "Invalid argument" errors from os.ReadFile #46849

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ghouscht opened this issue Jun 21, 2021 · 2 comments
Closed

not able to properly check "Invalid argument" errors from os.ReadFile #46849

ghouscht opened this issue Jun 21, 2021 · 2 comments

Comments

@ghouscht
Copy link

ghouscht commented Jun 21, 2021

What version of Go are you using (go version)?

$ go version
go version go1.16.5 linux/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
go env
GO111MODULE="on"
GOARCH="amd64"
GOBIN=""
GOCACHE="/tmp/go"
GOENV="/export/home/gostelit/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/work/go/pkg/mod"
GONOPROXY="*.pnet.ch"
GONOSUMDB="*.pnet.ch"
GOOS="linux"
GOPATH="/work/go"
GOPRIVATE="*.pnet.ch"
GOPROXY="https://proxy.golang.org"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.16.5"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/dev/null"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build3065707803=/tmp/go-build -gno-record-gcc-switches"

What did you do?

  1. Create a brige interface called test with sudo brctl addbr test
  2. Test if you can read the speed of this test bridge (should faild with Invalid argument:
cat /sys/class/net/test/speed
cat: /sys/class/net/test/speed: Invalid argument
  1. Execute the following go program and notice that it outputs this is a syscall.EINVAL error and nothing more.

main.go

package main

import (
        "errors"
        "fmt"
        "io/fs"
        "os"
        "syscall"
)

func main() {
        _, err := os.ReadFile("/sys/class/net/test/speed")
        if err != nil {
                if errors.Is(err, os.ErrInvalid) {
                        fmt.Println("this is an os.ErrInvalid error")
                }

                if errors.Is(err, fs.ErrInvalid) {
                        fmt.Println("this is an fs.ErrInvalid error")
                }

                if errors.Is(err, syscall.EINVAL) {
                        fmt.Println("this is a syscall.EINVAL error")
                }
        }
}

What did you expect to see?

According to the syscall Errno docs:

Errno values can be tested against error values from the os package using errors.Is. For example:

_, _, err := syscall.Syscall(...)
if errors.Is(err, fs.ErrNotExist) ...

I did expect that this example program also prints the two other lines. Also the documentation is a bit misleading since it says "you can test against error values from the os package..." but uses the fs package in the example.

Furthermore https://golang.org/pkg/io/fs/#pkg-variables states:

Generic file system errors. Errors returned by file systems can be tested against these errors using errors.Is.

and thus I'd expect to be able to check this error also with errors.Is(err,fs.ErrInvalid).

What did you see instead?

The program does not print the following two lines:

  • this is an os.ErrInvalid error
  • this is an fs.ErrInvalid error

it only prints:

  • this is a syscall.EINVAL error

Maybe this is just a misunderstanding from my side since I'm not a native English speaker. If so I think we should clarify this in the docs.

EDIT:
Related: #37627 refers to #30322 which had no progress for some time and is hard for me to relate to this issue.

@seankhliao
Copy link
Member

I think this is the same as #37627 / #30322 and it's just that nobody has put in the work to fix it yet

@seankhliao
Copy link
Member

Duplicate of #30322

@seankhliao seankhliao marked this as a duplicate of #30322 Jun 23, 2021
@golang golang locked and limited conversation to collaborators Jun 23, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants