Skip to content
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

regexp: RuneReader-related functions treat non-EOF errors the same as EOF #40509

Open
ailurarctos opened this issue Jul 31, 2020 · 2 comments
Open
Milestone

Comments

@ailurarctos
Copy link

@ailurarctos ailurarctos commented Jul 31, 2020

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

$ go version
go version go1.14.6 linux/amd64

Does this issue reproduce with the latest release?

Yes, go1.14.6.

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

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/me/.cache/go-build"
GOENV="/home/me/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/me/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/lib/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/lib/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
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-build878029847=/tmp/go-build -gno-record-gcc-switches"

What did you do?

Preamble: Sorry if this is known behavior for the regexp package. It was not obvious to me after reading the documentation.

I provided regexp.MatchReader with a RuneReader that always returns a non-EOF error (https://play.golang.org/p/_CVP4uouxy4):

package main

import (
	"errors"
	"fmt"
	"regexp"
)

var notEOF = errors.New("not EOF")

type notEOFErrRuneReader struct{}

func (notEOFErrRuneReader) ReadRune() (rune, int, error) {
	return 0, 0, notEOF
}

func main() {
	matched, err := regexp.MatchReader(".+", notEOFErrRuneReader{})
	fmt.Println(matched)
	fmt.Println(err)
}

What did you expect to see?

I expected regexp.MatchReader to return a non-nil error because the underlying RuneReader had an error.

What did you see instead?

regexp.MatchReader returned false (no match) and a nil error.

I also noticed the *Regexp methods that take a RuneReader do not return errors. I doubt much can be done about that other than potentially some documentation improvements.

I'm not sure if anything should be done about regexp.MatchReader but since the signature has an error it may be surprising that it only returns an error if the provided regular expression does not compile, not if the RuneReader has an error. If the behavior cannot be changed it might be good to have some documentation to let people know regexp.MatchReader may return false because all of the input was not available due to an error.

I was able to work around the issue by wrapping the rune reader and snooping on the results (https://play.golang.org/p/X-u5lkZ6hXw).

@cagedmantis cagedmantis added this to the Backlog milestone Aug 3, 2020
@cagedmantis
Copy link
Contributor

@cagedmantis cagedmantis commented Aug 3, 2020

@matloob
Copy link
Contributor

@matloob matloob commented Aug 7, 2020

My sense is that it's working as intended: mostly because of the connection between regexp.MatchReader and regexp.(*Regexp).MatchReader. I think it would be inconsistent for regexp.MatchReader to return an error if regexp.(*Regexp).MatchReader wouldn't (because it can't). I don't have an especially strong opinion about this though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.