Skip to content

I can modify string by unsafe and reflect, no panic. #49948

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

Closed
haochen233 opened this issue Dec 3, 2021 · 3 comments
Closed

I can modify string by unsafe and reflect, no panic. #49948

haochen233 opened this issue Dec 3, 2021 · 3 comments

Comments

@haochen233
Copy link

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

$ go version
go version go1.17 windows/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
set GO111MODULE=on
set GOARCH=amd64
set GOBIN=D:\software\go\bin
set GOCACHE=C:\Users\hao\AppData\Local\go-build
set GOENV=C:\Users\hao\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=D:\software\go\GOPATH\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=D:\software\go\GOPATH
set GOPRIVATE=
set GOPROXY=https://goproxy.io,direct
set GOROOT=D:\software\go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=D:\software\go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.17
set GCCGO=gccgo
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=D:\code\golang\personal\github.com\haochen233\go-stdlib-in-action\go.mod
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0 -fdebug-prefix-map=C:\Users\hao\AppData\Local\Temp\go-build803430058=/tmp/go-build -gno-record-gcc-switches
GOROOT/bin/go version: go version go1.17 windows/amd64
GOROOT/bin/go tool compile -V: compile version go1.17
gdb --version: GNU gdb (GDB) 8.1

What did you do?

I try to use package unsafe and reflect to convert string to []byte without memory allocation.

normal case:

package main

import (
	"reflect"
	"unsafe"
)

func main() {
	var str = "go"
	strHeader := (*reflect.StringHeader)(unsafe.Pointer(&str))

	sliceHeader := &reflect.SliceHeader{
		Data: strHeader.Data,
		Len:  strHeader.Len,
		Cap:  strHeader.Len,
	}
	
	slice := *(*[]byte)(unsafe.Pointer(sliceHeader))
	slice[0] = 'G'
	println(str)
}

This will panic, because string are not modifiable.

But, there's a confusing case here

package main

import (
	"reflect"
	"unsafe"
)

func main() {
	var str = "go"

	// string concatenation operation.
	str += "Go"

	strHeader := (*reflect.StringHeader)(unsafe.Pointer(&str))
	sliceHeader := &reflect.SliceHeader{
		Data: strHeader.Data,
		Len:  strHeader.Len,
		Cap:  strHeader.Len,
	}

	slice := *(*[]byte)(unsafe.Pointer(sliceHeader))
	slice[0] = 'G'
	println(str)
}

I successfully modified the string, no panic. I'm a little confused.

What did you expect to see?

What did you see instead?

@haochen233 haochen233 changed the title I can modify string through package I can modify string by unsafe and reflect, no panic. Dec 3, 2021
@ALTree
Copy link
Member

ALTree commented Dec 3, 2021

unsafe lets do you unsafe things, but there's no guarantee that the program will crash or panic if you happen to use it wrong. Things will work sometimes, and break some other times.

@seankhliao
Copy link
Member

closing as not a bug

@zorklabs
Copy link

Nothing special.
The first snippet not working because compiler "mark" string as constant and put it in constant address space where changed are prohibited.

@golang golang locked and limited conversation to collaborators Jun 24, 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

5 participants