Description
What version of Go are you using (go version
)?
$ go version go version go1.15.5 darwin/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 envGO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/subhamsarkar/Library/Caches/go-build"
GOENV="/Users/subhamsarkar/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/subhamsarkar/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/subhamsarkar/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/Cellar/go/1.15.5/libexec"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.15.5/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
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 -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/n2/w00w99g93cj26xl42msl6lc80000gp/T/go-build468451499=/tmp/go-build -gno-record-gcc-switches -fno-common"
What did you do?
Trim
and friends (TrimXXX
) in the bytes
package seem to unintentionally overwrite slices. Well, this could be working as designed but do refer:
- bytes: appending to a single slice from Split output can affect other slices of the output #21149
- regexp: Find makes it easy to unintentionally overwrite slices #30169
So, based on CLs that fixed the issues listed above, I think it'd be appropriate to match the capacity to the length so that it could be avoided and match the behaviour of other functions.
package main
import (
"bytes"
"fmt"
)
// func FixTrimSuffix(s, suffix []byte) []byte {
// if bytes.HasSuffix(s, suffix) {
// // Match capacity and length. Fixed bytes.TrimSuffix
// return s[: len(s)-len(suffix) : len(s)-len(suffix)]
// }
// return s
// }
func main() {
b := []byte{'h', 'e', 'l', 'l', 'o'}
t := []byte{'l', 'o'}
fmt.Printf("b: %s\nt: %s\n", b, t)
fmt.Printf("\n~ TrimSuffix t from b\n")
bytesTrim := bytes.TrimSuffix(b, t)
fmt.Printf("b: %s\nbytesTrim: %s\n", b, bytesTrim)
fmt.Printf("\n~ Append xy to bytesTrim\n")
bytesTrim = append(bytesTrim, 'x', 'y')
fmt.Printf("b: %s\nbytesTrim: %s\n", b, bytesTrim)
}
Link to the playground: https://play.golang.org/p/yGJxI_Jnkk6
What did you expect to see?
b: hello
t: lo
~ TrimSuffix t from b
b: hello
bytesTrim: hel
~ Append xy to bytesTrim
b: hello
bytesTrim: helxy
What did you see instead?
b: hello
t: lo
~ TrimSuffix t from b
b: hello
bytesTrim: hel
~ Append xy to bytesTrim
b: helxy
bytesTrim: helxy