Skip to content

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

Closed as not planned
Closed as not planned
@firelizzard18

Description

@firelizzard18

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/[email protected]/internal/colltab/iter.go:104 +0x239
golang.org/x/text/collate.(*iter).nextPrimary(0xc000116060)
	/tmp/gopath1836550904/pkg/mod/golang.org/x/[email protected]/collate/collate.go:254 +0x35
golang.org/x/text/collate.compareLevel(0x4d4228, 0xc000116060, 0xc0001168d0)
	/tmp/gopath1836550904/pkg/mod/golang.org/x/[email protected]/collate/collate.go:143 +0x54
golang.org/x/text/collate.(*Collator).compare(0xc000116000)
	/tmp/gopath1836550904/pkg/mod/golang.org/x/[email protected]/collate/collate.go:162 +0x45
golang.org/x/text/collate.(*Collator).CompareString(0xc000116000, {0x4c79d4, 0x6}, {0x4c756f, 0x3})
	/tmp/gopath1836550904/pkg/mod/golang.org/x/[email protected]/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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions