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

x/text/transform: errInconsistentByteCount should be returned by too big nSrc #31486

Open
acomagu opened this issue Apr 16, 2019 · 1 comment

Comments

@acomagu
Copy link

commented Apr 16, 2019

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

$ go version
go version go1.12 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
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/yuki/.cache/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/yuki/.local/share/go"
GOPROXY=""
GORACE=""
GOROOT="/usr/lib/go"
GOTMPDIR=""
GOTOOLDIR="/usr/lib/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/yuki/.local/src/golang.org/x/text/go.mod"
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-build370827352=/tmp/go-build -gno-record-gcc-switches"

What did you do?

package main

import (
	"bytes"

	"golang.org/x/text/transform"
)

type T struct{ transform.NopResetter }

func (*T) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) {
	return 0, 4, transform.ErrShortDst
}

func main() {
	buf := make([]byte, 3)
	fmt.Println(transform.NewReader(bytes.NewReader([]byte("abc")), &T{}).Read(buf))
}

The point is that the Transform() returns invalid nSrc.

What did you expect to see?

The Read() should return transform.errInconsistentByteCount because the Transform() returns invalid nSrc(nSrc > len(src)). The code comment says

errInconsistentByteCount means that Transform returned success (nil error) but also returned nSrc inconsistent with the src argument.

The error is suitable for this case.

What did you see instead?

It panics with slice bounds out of range.

panic: runtime error: slice bounds out of range

goroutine 1 [running]:
golang.org/x/text/transform.(*Reader).Read(0xc00003c700, 0xc00003c6dd, 0x3, 0x3, 0x0, 0xe0c843aa8a3c7960, 0xc00003c6f0)
        /home/yuki/.local/src/golang.org/x/text/transform/transform.go:166 +0x622
main.main()
        /home/yuki/.local/src/golang.org/x/text/transform/test/main.go:17 +0x20e
exit status 2

The stack trace points the internal of the library, not the implementation of Transformer. This behavior makes difficult to debug because the real problem is in the use of transform package, but the error doesn't suggest that.

@gopherbot gopherbot added this to the Unreleased milestone Apr 16, 2019

@acomagu acomagu changed the title x/text/transform: errInconsistentByteCount should be returned for too big nSrc x/text/transform: errInconsistentByteCount should be returned by too big nSrc Apr 16, 2019

@julieqiu

This comment has been minimized.

Copy link

commented May 28, 2019

/cc @mpvl

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.