golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
122.89k stars 17.52k forks source link

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

Open acomagu opened 5 years ago

acomagu commented 5 years ago

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.

julieqiu commented 5 years ago

/cc @mpvl