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: Find makes it easy to unintentionally overwrite slices #30169

Closed
campoy opened this issue Feb 11, 2019 · 1 comment
Closed

regexp: Find makes it easy to unintentionally overwrite slices #30169

campoy opened this issue Feb 11, 2019 · 1 comment
Labels
FrozenDueToAge NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made.
Milestone

Comments

@campoy
Copy link
Contributor

campoy commented Feb 11, 2019

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

$ go version
go version devel +e1b49ad608 Sun Feb 10 18:40:06 2019 +0000 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 env
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/francesc/Library/Caches/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/francesc"
GOPROXY=""
GORACE=""
GOROOT="/Users/francesc/go"
GOTMPDIR=""
GOTOOLDIR="/Users/francesc/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/francesc/go/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 -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/tm/816df9c9399_1fnz90k6gfq80000gn/T/go-build072052763=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

I created a little program using the slices returned by FindAll by appending some extra characters before printing them.

package main

import (
	"fmt"
	"regexp"
)

func main() {
	input := []byte(`one two three four five`)

	r := regexp.MustCompile("[a-zA-Z]+")

	ss := r.FindAll(input, -1)
	for _, s := range ss {
		fmt.Printf("%s\n", append(s, '.', '.', '.'))
	}
}

What did you expect to see?

I expected the output to be:

one...
two...
three...
four...
five...

What did you see instead?

The output was instead:

one...
..o...
..ree...
..ur...
..ve...

This is similar to the fixes already done in bytes.Fields.

@gopherbot
Copy link

Change https://golang.org/cl/161877 mentions this issue: regexp: limit the capacity of slices of bytes returned by FindX

@bradfitz bradfitz added the NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. label Feb 11, 2019
@bradfitz bradfitz added this to the Go1.13 milestone Feb 11, 2019
@golang golang locked and limited conversation to collaborators Feb 27, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made.
Projects
None yet
Development

No branches or pull requests

3 participants