英文:
slice bounds out of range when using unsafe.Pointer
问题
当我使用类似于b[1:2]的语法将[]byte转换为字符串,然后再转换回[]byte时,我遇到了一种奇怪的恐慌情绪。
我的Go版本是go1.7.3 darwin/amd64。以下是详细的代码。
package main
import (
"reflect"
"unsafe"
"fmt"
)
func BytesToString(b []byte) string {
bh := (*reflect.SliceHeader)(unsafe.Pointer(&b))
sh := reflect.StringHeader{bh.Data, bh.Len}
return *(*string)(unsafe.Pointer(&sh))
}
func StringToBytes(s string) []byte {
sh := (*reflect.StringHeader)(unsafe.Pointer(&s))
bh := reflect.SliceHeader{sh.Data, sh.Len, 0}
return *(*[]byte)(unsafe.Pointer(&bh))
}
func main() {
b := []byte{'b', 'y', 't', 'e'}
// No1 here you can trim []byte using b[1:2]
_ = b[1:2]
fmt.Println("No1")
// convert []byte to string
s := BytesToString(b)
// convert string to []byte
b = StringToBytes(s)
// create new []byte variant using content of b
bb := make([]byte, len(b))
for i := 0; i < len(b); i++ {
bb[i] = b[i]
}
// No2 here you also can trim []byte using bb[1:2]
_ = bb[1:2]
fmt.Println("No2")
// No3 here you can not trim []byte. I don't know why. why?
_ = b[1:2]
fmt.Println("No3")
}
运行这段代码,会得到以下错误信息:
No1
No2
panic: runtime error: slice bounds out of range
goroutine 1 [running]:
panic(0x8f060, 0xc42000a100)
/usr/local/Cellar/go/1.7.3/libexec/src/runtime/panic.go:500 +0x1a1
main.main()
/tmp/unsafe.go:45 +0x274
exit status 2
我对导致这个恐慌的原因很好奇。
英文:
I am faced up with a curious panic when I trim a byte array using syntax like b[1:2] which is converted from []byte to string and then back to []byte.
My go version is go1.7.3 darwin/amd64. What belows is the detail code.
package main
import (
"reflect"
"unsafe"
"fmt"
)
func BytesToString(b []byte) string {
bh := (*reflect.SliceHeader)(unsafe.Pointer(&b))
sh := reflect.StringHeader{bh.Data, bh.Len}
return *(*string)(unsafe.Pointer(&sh))
}
func StringToBytes(s string) []byte {
sh := (*reflect.StringHeader)(unsafe.Pointer(&s))
bh := reflect.SliceHeader{sh.Data, sh.Len, 0}
return *(*[]byte)(unsafe.Pointer(&bh))
}
func main() {
b := []byte{'b', 'y', 't', 'e'}
// No1 here you can trim []byte using b[1:2]
_ = b[1:2]
fmt.Println("No1")
// convert []byte to string
s := BytesToString(b)
// convert string to []byte
b = StringToBytes(s)
// create new []byte variant using content of b
bb := make([]byte, len(b))
for i := 0; i < len(b); i++ {
bb[i] = b[i]
}
// No2 here you also can trim []byte using bb[1:2]
_ = bb[1:2]
fmt.Println("No2")
// No3 here you can not trim []byte. I don't know why. why?
_ = b[1:2]
fmt.Println("No3")
}
Run this code, and get error as follows:
No1
No2
panic: runtime error: slice bounds out of range
goroutine 1 [running]:
panic(0x8f060, 0xc42000a100)
/usr/local/Cellar/go/1.7.3/libexec/src/runtime/panic.go:500 +0x1a1
main.main()
/tmp/unsafe.go:45 +0x274
exit status 2
I'm curious about what caused this panic?
答案1
得分: 3
StringToBytes
函数创建的切片的容量为零。
for
循环不会引发恐慌,因为索引表达式会检查len(b)
。
表达式b[1:2]
会引发恐慌,因为切片表达式会检查cap(b)
。
修复的方法之一是将容量设置为字符串的长度:
func StringToBytes(s string) []byte {
sh := (*reflect.StringHeader)(unsafe.Pointer(&s))
bh := reflect.SliceHeader{sh.Data, sh.Len, sh.Len}
return *(*[]byte)(unsafe.Pointer(&bh))
}
英文:
The capacity of the slice created by StringToBytes
is zero.
The for loop does not panic because index expressions check len(b)
.
The expression b[1:2]
panics because slice expressions check cap(b)
.
One fix is to set capacity to the length of the string:
func StringToBytes(s string) []byte {
sh := (*reflect.StringHeader)(unsafe.Pointer(&s))
bh := reflect.SliceHeader{sh.Data, sh.Len, sh.Len}
return *(*[]byte)(unsafe.Pointer(&bh))
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论