Skip to content

Commit

Permalink
*: reduce unsafe usage (#8482)
Browse files Browse the repository at this point in the history
ref #8475

Signed-off-by: Ryan Leung <[email protected]>

Co-authored-by: ti-chi-bot[bot] <108142056+ti-chi-bot[bot]@users.noreply.github.com>
  • Loading branch information
rleungx and ti-chi-bot[bot] authored Aug 6, 2024
1 parent 838ee79 commit 3b0851a
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 44 deletions.
2 changes: 1 addition & 1 deletion pkg/core/region.go
Original file line number Diff line number Diff line change
Expand Up @@ -2143,7 +2143,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))
}

// 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)
}

0 comments on commit 3b0851a

Please sign in to comment.