使用`unsafe.Pointer`时,切片边界超出范围。

huangapple go评论85阅读模式
英文:

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 (
            &quot;reflect&quot;
            &quot;unsafe&quot;
            &quot;fmt&quot;

)

func BytesToString(b []byte) string {
    bh := (*reflect.SliceHeader)(unsafe.Pointer(&amp;b))
    sh := reflect.StringHeader{bh.Data, bh.Len}
    return *(*string)(unsafe.Pointer(&amp;sh))
}

func StringToBytes(s string) []byte {
    sh := (*reflect.StringHeader)(unsafe.Pointer(&amp;s))
    bh := reflect.SliceHeader{sh.Data, sh.Len, 0}
    return *(*[]byte)(unsafe.Pointer(&amp;bh))
}

func main() {
b := []byte{&#39;b&#39;, &#39;y&#39;, &#39;t&#39;, &#39;e&#39;}

// No1  here you can trim []byte using b[1:2]
_ = b[1:2]
fmt.Println(&quot;No1&quot;)

// 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 &lt; len(b); i++ {
	bb[i] = b[i]
}

// No2 here you also can trim []byte using bb[1:2]
_ = bb[1:2]
fmt.Println(&quot;No2&quot;)

// No3 here you can not trim []byte. I don&#39;t know why. why?
_ = b[1:2]
fmt.Println(&quot;No3&quot;)

}

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(&amp;s))
    bh := reflect.SliceHeader{sh.Data, sh.Len, sh.Len}
    return *(*[]byte)(unsafe.Pointer(&amp;bh))
}

huangapple
  • 本文由 发表于 2017年1月11日 20:33:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/41591097.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定