From 3b0851ad4ed7612095cd7452a920ab44291c50af Mon Sep 17 00:00:00 2001 From: Ryan Leung Date: Tue, 6 Aug 2024 11:54:38 +0800 Subject: [PATCH] *: reduce unsafe usage (#8482) ref tikv/pd#8475 Signed-off-by: Ryan Leung Co-authored-by: ti-chi-bot[bot] <108142056+ti-chi-bot[bot]@users.noreply.github.com> --- pkg/core/region.go | 2 +- pkg/encryption/crypter.go | 10 +++++----- pkg/storage/hot_region_storage.go | 9 ++++----- pkg/utils/logutil/log.go | 3 +-- pkg/utils/typeutil/conversion.go | 17 ----------------- pkg/utils/typeutil/conversion_test.go | 14 -------------- 6 files changed, 11 insertions(+), 44 deletions(-) diff --git a/pkg/core/region.go b/pkg/core/region.go index 9768a258889..53268589c8a 100644 --- a/pkg/core/region.go +++ b/pkg/core/region.go @@ -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 diff --git a/pkg/encryption/crypter.go b/pkg/encryption/crypter.go index b1f8631ae26..7e69854c5a8 100644 --- a/pkg/encryption/crypter.go +++ b/pkg/encryption/crypter.go @@ -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. @@ -106,7 +106,7 @@ 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 { @@ -114,7 +114,7 @@ func NewDataKey( "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 diff --git a/pkg/storage/hot_region_storage.go b/pkg/storage/hot_region_storage.go index 50fa7455f44..d323b40d435 100644 --- a/pkg/storage/hot_region_storage.go +++ b/pkg/storage/hot_region_storage.go @@ -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" ) @@ -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] } @@ -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 } diff --git a/pkg/utils/logutil/log.go b/pkg/utils/logutil/log.go index c7a9ac2f3b7..4854fd7ac40 100644 --- a/pkg/utils/logutil/log.go +++ b/pkg/utils/logutil/log.go @@ -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" ) @@ -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 diff --git a/pkg/utils/typeutil/conversion.go b/pkg/utils/typeutil/conversion.go index dab12a52d9e..128c7a887a4 100644 --- a/pkg/utils/typeutil/conversion.go +++ b/pkg/utils/typeutil/conversion.go @@ -16,7 +16,6 @@ package typeutil import ( "encoding/binary" - "unsafe" "github.com/tikv/pd/pkg/errs" ) @@ -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)) -} diff --git a/pkg/utils/typeutil/conversion_test.go b/pkg/utils/typeutil/conversion_test.go index e69eeb57e23..7b17cfcbe2c 100644 --- a/pkg/utils/typeutil/conversion_test.go +++ b/pkg/utils/typeutil/conversion_test.go @@ -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) -}