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

*: reduce unsafe usage #8482

Merged
merged 2 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion pkg/core/region.go
Original file line number Diff line number Diff line change
Expand Up @@ -2142,7 +2142,7 @@ func HexRegionKey(key []byte) []byte {
// HexRegionKeyStr converts region key to hex format. Used for formatting region in
// logs.
func HexRegionKeyStr(key []byte) string {
return typeutil.BytesToString(HexRegionKey(key))
return string(HexRegionKey(key))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you refer to relative go changes?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

// RegionToHexMeta converts a region meta's keys to hex format. Used for formatting
Expand Down
10 changes: 5 additions & 5 deletions pkg/encryption/crypter.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ import (
"crypto/rand"
"encoding/binary"
"io"
"unsafe"

"github.com/pingcap/kvproto/pkg/encryptionpb"
"github.com/tikv/pd/pkg/errs"
)

const (
ivLengthCTR = 16
ivLengthGCM = 12
ivLengthCTR = 16
ivLengthGCM = 12
keyIDBufSize = 8
)

// CheckEncryptionMethodSupported check whether the encryption method is currently supported.
Expand Down Expand Up @@ -106,15 +106,15 @@ func NewDataKey(
if err != nil {
return
}
keyIDBufSize := unsafe.Sizeof(uint64(0))

keyIDBuf := make([]byte, keyIDBufSize)
n, err := io.ReadFull(rand.Reader, keyIDBuf)
if err != nil {
err = errs.ErrEncryptionNewDataKey.Wrap(err).GenWithStack(
"fail to generate data key id")
return
}
if n != int(keyIDBufSize) {
if n != keyIDBufSize {
err = errs.ErrEncryptionNewDataKey.GenWithStack(
"no enough random bytes to generate data key id, bytes %d", n)
return
Expand Down
9 changes: 4 additions & 5 deletions pkg/storage/hot_region_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import (
"github.com/tikv/pd/pkg/storage/kv"
"github.com/tikv/pd/pkg/utils/logutil"
"github.com/tikv/pd/pkg/utils/syncutil"
"github.com/tikv/pd/pkg/utils/typeutil"
"go.uber.org/zap"
)

Expand Down Expand Up @@ -267,8 +266,8 @@ func (h *HotRegionStorage) packHistoryHotRegions(historyHotRegions []HistoryHotR
if err != nil {
return err
}
historyHotRegions[i].StartKey = typeutil.BytesToString(region.StartKey)
historyHotRegions[i].EndKey = typeutil.BytesToString(region.EndKey)
historyHotRegions[i].StartKey = string(region.StartKey)
historyHotRegions[i].EndKey = string(region.EndKey)
key := HotRegionStorePath(hotRegionType, historyHotRegions[i].UpdateTime, historyHotRegions[i].RegionID)
h.batchHotInfo[key] = &historyHotRegions[i]
}
Expand Down Expand Up @@ -386,8 +385,8 @@ func (it *HotRegionStorageIterator) Next() (*HistoryHotRegion, error) {
if err := encryption.DecryptRegion(region, it.encryptionKeyManager); err != nil {
return nil, err
}
message.StartKey = typeutil.BytesToString(region.StartKey)
message.EndKey = typeutil.BytesToString(region.EndKey)
message.StartKey = string(region.StartKey)
message.EndKey = string(region.EndKey)
message.EncryptionMeta = nil
return &message, nil
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/utils/logutil/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (

"github.com/pingcap/log"
"github.com/tikv/pd/pkg/errs"
"github.com/tikv/pd/pkg/utils/typeutil"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
Expand Down Expand Up @@ -223,7 +222,7 @@ func RedactBytes(arg []byte) []byte {
return []byte("?")
case RedactInfoLogMarker:
// Use unsafe conversion to avoid copy.
return typeutil.StringToBytes(redactInfo(typeutil.BytesToString(arg)))
return []byte(redactInfo(string(arg)))
default:
}
return arg
Expand Down
17 changes: 0 additions & 17 deletions pkg/utils/typeutil/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package typeutil

import (
"encoding/binary"
"unsafe"

"github.com/tikv/pd/pkg/errs"
)
Expand Down Expand Up @@ -69,19 +68,3 @@ func JSONToUint64Slice(from any) ([]uint64, bool) {
}
return to, true
}

// BytesToString converts slice of bytes to string without copy.
func BytesToString(b []byte) string {
if len(b) == 0 {
return ""
}
return unsafe.String(unsafe.SliceData(b), len(b))
}

// StringToBytes converts string to slice of bytes without copy.
func StringToBytes(s string) []byte {
if len(s) == 0 {
return nil
}
return unsafe.Slice(unsafe.StringData(s), len(s))
}
14 changes: 0 additions & 14 deletions pkg/utils/typeutil/conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,3 @@ func TestJSONToUint64Slice(t *testing.T) {
re.False(ok)
re.Nil(res)
}

func TestBytesToString(t *testing.T) {
re := require.New(t)
str := "hello"
b := []byte(str)
re.Equal(str, BytesToString(b))
}

func TestStringToBytes(t *testing.T) {
re := require.New(t)
str := "hello"
b := StringToBytes(str)
re.Equal([]byte(str), b)
}