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/collate: CompareString is not threadsafe #57314

Closed
firelizzard18 opened this issue Dec 14, 2022 · 2 comments
Closed

x/text/collate: CompareString is not threadsafe #57314

firelizzard18 opened this issue Dec 14, 2022 · 2 comments

Comments

@firelizzard18
Copy link
Contributor

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

$ go version
go version go1.19.2 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
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/lib/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/lib/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.19.2"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="x86_64-pc-linux-gnu-gcc"
CXX="x86_64-pc-linux-gnu-g++"
CGO_ENABLED="1"
GOMOD="/dev/null"
GOWORK=""
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 -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build1032427755=/tmp/go-build -gno-record-gcc-switches"

What did you do?

https://go.dev/play/p/9j5NigpTkVg

package main

import (
	"sync"

	"golang.org/x/text/collate"
	"golang.org/x/text/language"
)

var enUsIgnoreCase = collate.New(language.AmericanEnglish, collate.IgnoreCase)

func main() {
	const N = 10_000
	wg := new(sync.WaitGroup)
	wg.Add(N)
	for i := 0; i < N; i++ {
		go func() { defer wg.Done(); enUsIgnoreCase.CompareString("foobar", "baz") }()
	}
	wg.Wait()
}

What did you expect to see?

Nothing (no error).

What did you see instead?

panic: runtime error: index out of range [-1]

goroutine 1349 [running]:
golang.org/x/text/internal/colltab.(*Iter).Next(0xc000116860)
	/tmp/gopath1836550904/pkg/mod/golang.org/x/text@v0.5.0/internal/colltab/iter.go:104 +0x239
golang.org/x/text/collate.(*iter).nextPrimary(0xc000116060)
	/tmp/gopath1836550904/pkg/mod/golang.org/x/text@v0.5.0/collate/collate.go:254 +0x35
golang.org/x/text/collate.compareLevel(0x4d4228, 0xc000116060, 0xc0001168d0)
	/tmp/gopath1836550904/pkg/mod/golang.org/x/text@v0.5.0/collate/collate.go:143 +0x54
golang.org/x/text/collate.(*Collator).compare(0xc000116000)
	/tmp/gopath1836550904/pkg/mod/golang.org/x/text@v0.5.0/collate/collate.go:162 +0x45
golang.org/x/text/collate.(*Collator).CompareString(0xc000116000, {0x4c79d4, 0x6}, {0x4c756f, 0x3})
	/tmp/gopath1836550904/pkg/mod/golang.org/x/text@v0.5.0/collate/collate.go:126 +0x145
main.main.func1()
	/tmp/sandbox4270487653/prog.go:17 +0x77
created by main.main
	/tmp/sandbox4270487653/prog.go:17 +0x3d

Program exited.
@gopherbot gopherbot added this to the Unreleased milestone Dec 14, 2022
@firelizzard18
Copy link
Contributor Author

I see no indication in the collate package documentation that it is not threadsafe, so I assumed I could define the collation as a package variable.

@seankhliao
Copy link
Member

In general, unless explicitly documented to be safe for concurrent use (eg text/template.Template.Execute, that is not a safe assumption to make.

see also #3611 (comment)

@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Dec 14, 2022
@golang golang locked and limited conversation to collaborators Dec 14, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants