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

utils: Fix possible misuse of reflect.SliceHeader vet error when go 1.16.6 #139 #141

Merged
merged 1 commit into from
Jul 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions utils/unsafe.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,18 @@ import (
)

// BytesToString casts slice to string without copy
func BytesToString(b []byte) (s string) {
func BytesToString(b []byte) string {
andyli029 marked this conversation as resolved.
Show resolved Hide resolved
if len(b) == 0 {
return ""
}

bh := (*reflect.SliceHeader)(unsafe.Pointer(&b))
sh := reflect.StringHeader{Data: bh.Data, Len: bh.Len}

return *(*string)(unsafe.Pointer(&sh))
p := unsafe.Pointer((*reflect.StringHeader)(unsafe.Pointer(&b)).Data)
var s string
sh := (*reflect.SliceHeader)(unsafe.Pointer(&s))
sh.Data = uintptr(p)
sh.Cap = len(b)
sh.Len = len(b)
return s
}

// StringToBytes casts string to slice without copy
Expand All @@ -39,8 +42,11 @@ func StringToBytes(s string) []byte {
return []byte{}
}

sh := (*reflect.StringHeader)(unsafe.Pointer(&s))
bh := reflect.SliceHeader{Data: sh.Data, Len: sh.Len, Cap: sh.Len}

return *(*[]byte)(unsafe.Pointer(&bh))
p := unsafe.Pointer((*reflect.StringHeader)(unsafe.Pointer(&s)).Data)
var b []byte
sh := (*reflect.SliceHeader)(unsafe.Pointer(&b))
sh.Data = uintptr(p)
sh.Cap = len(s)
sh.Len = len(s)
return b
}
61 changes: 61 additions & 0 deletions utils/unsafe_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
Copyright 2021 RadonDB.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package utils

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestBytesToString(t *testing.T) {
{
bs := []byte{0x61, 0x62}
want := "ab"
got := BytesToString(bs)
assert.Equal(t, want, got)
}

{
bs := []byte{}
want := ""
got := BytesToString(bs)
assert.Equal(t, want, got)
}
}

func TestSting(t *testing.T) {
{
want := []byte{0x61, 0x62}
got := StringToBytes("ab")
assert.Equal(t, want, got)
}

{
want := []byte{}
got := StringToBytes("")
assert.Equal(t, want, got)
}
}

func TestStingToBytes(t *testing.T) {
{
want := []byte{0x53, 0x45, 0x4c, 0x45, 0x43, 0x54, 0x20, 0x2a, 0x20, 0x46, 0x52, 0x4f, 0x4d, 0x20, 0x74, 0x32}
got := StringToBytes("SELECT * FROM t2")
assert.Equal(t, want, got)
}
}