Skip to content

sync: Different behavior between sync.Map and normal map paired with unsafe.String #70536

Closed as not planned
@chrooti

Description

@chrooti

Go version

go1.23.3 linux/amd64

Output of go env in your module/workspace:

GO111MODULE=''
GOARCH='amd64'
GOBIN=''
GOCACHE='/home/myuser/.cache/go-build'
GOENV='/home/myuser/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='/home/myuser/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/myuser/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/usr/lib/go'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/lib/go/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.23.3'
GODEBUG=''
GOTELEMETRY='off'
GOTELEMETRYDIR='/home/myuser/.config/go/telemetry'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='gcc'
CXX='g++'
CGO_ENABLED='1'
GOMOD='/Projects/myproject/go.mod'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build1366124205=/tmp/go-build -gno-record-gcc-switches'

What did you do?

  • make a new byte slice
  • make a map[string]int
  • add the byte slice to the map by casting it to string via unsafe.String and unsafe.SliceData
  • change the first character of the slice
  • fetch the map entry by casting the newly changed slice via unsafe as previously described
  • repeat the experiment by using sync.Map

https://go.dev/play/p/70KMafesB18

What did you see happen?

while using the standard map: the value is found, regardless of the content of the string
while using sync.Map: the value is not found

What did you expect to see?

consistent behavior between the two cases. My suspect is that sync.Map checks for "actual" equality while the standard map checks just for the Data pointer to be equal.
I'm not entirely sure this is a bug, given the use of unsafe but it was surprising.

Metadata

Metadata

Assignees

No one assigned

    Labels

    compiler/runtimeIssues related to the Go compiler and/or runtime.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions