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

The debug result is different with go run result ( using unsafe. Pointer, got []byte cap = 0 and len >0 ) #40541

Closed
undom opened this issue Aug 3, 2020 · 1 comment

Comments

@undom
Copy link
Contributor

undom commented Aug 3, 2020

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

$ go version
go version go1.13.9 darwin/amd64

Does this issue reproduce with the latest release?

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE="on"
GOARCH="amd64"
GOBIN="/xxx/go/bin"
GOCACHE="/xxx/Library/Caches/go-build"
GOENV="/xxx/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=" -mod="
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/xxx/go"
GOPRIVATE=""
GOPROXY="https://goproxy.cn/"
GOROOT="/xxx/.g/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/xxx/.g/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/secret/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/3p/qnjdkh4j6z1f3g1rnbrjzsmm0000gn/T/go-build668244624=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

Debug using delve

package main

import (
   "unicode/utf8"
   "unsafe"
)

func main() {
   b := stringToBytes("模板ID")
   println(len(b))
   println(cap(b))
   r, width := utf8.DecodeRune(b[6:])
   println(r)
   println(width)
   println(b[0:5])
}

func stringToBytes(s string) []byte {
   return *(*[]byte)(unsafe.Pointer(&s))
}

Run directly result

GOROOT=/xxx/.g/versions/1.13.9 #gosetup
GOPATH=/xxx/go #gosetup
/xxx/.g/versions/1.13.9/bin/go build -i -o /xxx/GolandProjects/x/bin/go_build_main_go /xxx/GolandProjects/x/cmd/main.go #gosetup
/xxx/GolandProjects/x/bin/go_build_main_go #gosetup
8
824633993168
73
1
[5/824633993168]0x107187a

debug result

GOROOT=/xxx/.g/versions/1.13.9 #gosetup
GOPATH=/xxx/go #gosetup
/xxx/.g/versions/1.13.9/bin/go build -o /xxx/GolandProjects/x/bin/go_build_main_go -gcflags "all=-N -l" /xxx/GolandProjects/x/cmd/main.go #gosetup
/Applications/GoLand.app/Contents/plugins/go/lib/dlv/mac/dlv --listen=localhost:65499 --headless=true --api-version=2 --check-go-version=false --only-same-user=false exec /xxx/GolandProjects/x/bin/go_build_main_go -- #gosetup
API server listening at: 127.0.0.1:65499
debugserver-@(#)PROGRAM:LLDB  PROJECT:lldb-1103.0.22
 for x86_64.
Got a connection, launched process /xxx/GolandProjects/x/bin/go_build_main_go (pid = 28332).
8
0
65533
1
panic: runtime error: slice bounds out of range [:5] with capacity 0

goroutine 1 [running]:
main.main()
        /xxx/GolandProjects/x/cmd/main.go:15 +0x1c2
Exiting.

What did you expect to see?

  • The Result same with run directly
  • The unsafe.Pointer didnt return []byte with cap = 0 and len > 0

What did you see instead?

  • The debug result is different with run directly result
  • I got a []byte with cap = 0 and len > 0

I submited an issue on delve, somebody say its not the delve's problem,Im confused
go-delve/delve#2115

@davecheney
Copy link
Contributor

Your code is not sound, you cannot convert a string to a []byte safely.

I'm going to close this issue as it does not seem like there is a bug in Go. Unlike many projects, the Go project does not use GitHub Issues for general discussion or asking questions. GitHub Issues are used for tracking bugs and proposals only.

For asking questions, see:

@golang golang locked and limited conversation to collaborators Aug 3, 2021
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