diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 63f4802..6f58b89 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -109,7 +109,7 @@ jobs: go-version: ${{ env.GO_VERSION }} - name: Install StaticCheck - run: go install honnef.co/go/tools/cmd/staticcheck@2022.1 + run: go install honnef.co/go/tools/cmd/staticcheck@2024.1.1 - name: Cache Go Dependencies uses: actions/cache@v2 diff --git a/pkg/utils/slice.go b/pkg/utils/slice.go index 3e8cb22..28f3c4a 100644 --- a/pkg/utils/slice.go +++ b/pkg/utils/slice.go @@ -74,8 +74,8 @@ func SliceRandList(min, max int) []int { } length := max - min + 1 t0 := time.Now() - rand.Seed(int64(t0.Nanosecond())) - list := rand.Perm(length) + r := rand.New(rand.NewSource(int64(t0.Nanosecond()))) + list := r.Perm(length) for index := range list { list[index] += min } diff --git a/pkg/utils/strings.go b/pkg/utils/strings.go index 1e5ab87..65c430a 100755 --- a/pkg/utils/strings.go +++ b/pkg/utils/strings.go @@ -17,7 +17,6 @@ limitations under the License. package utils import ( - "reflect" "strings" "unsafe" ) @@ -66,16 +65,17 @@ func Substr(str string, start, length int) string { return string(rs[start:end]) } -func String2Bytes(s string) (b []byte) { - bs := (*reflect.SliceHeader)(unsafe.Pointer(&b)) - ss := (*reflect.StringHeader)(unsafe.Pointer(&s)) - bs.Data = ss.Data - bs.Len = ss.Len - bs.Cap = ss.Len - return b +func String2Bytes(s string) []byte { + var ret []byte + if len(s) == 0 { + return ret + } + return unsafe.Slice(unsafe.StringData(s), len(s)) } -func Bytes2String(b []byte) (s string) { - s = *(*string)(unsafe.Pointer(&b)) - return s +func Bytes2String(b []byte) string { + if len(b) == 0 { + return "" + } + return unsafe.String(&b[0], len(b)) }