diff --git a/executor/show.go b/executor/show.go index c29bb10c1de35..99e9d80121a01 100644 --- a/executor/show.go +++ b/executor/show.go @@ -69,6 +69,7 @@ import ( "github.com/pingcap/tidb/util/memory" "github.com/pingcap/tidb/util/sem" "github.com/pingcap/tidb/util/set" + "github.com/pingcap/tidb/util/slice" "github.com/pingcap/tidb/util/sqlexec" "github.com/pingcap/tidb/util/stringutil" "golang.org/x/exp/slices" @@ -313,8 +314,7 @@ func (e *ShowExec) fetchShowBind() error { } else { tmp = domain.GetDomain(e.ctx).BindHandle().GetAllBindRecord() } - bindRecords := make([]*bindinfo.BindRecord, len(tmp)) - copy(bindRecords, tmp) + bindRecords := slice.Copy(tmp) // Remove the invalid bindRecord. ind := 0 for _, bindData := range bindRecords { diff --git a/util/slice/slice.go b/util/slice/slice.go index 078772d58c15a..12738614a115f 100644 --- a/util/slice/slice.go +++ b/util/slice/slice.go @@ -39,3 +39,16 @@ func AllOf(s interface{}, p func(int) bool) bool { } return NoneOf(s, np) } + +// Copy is a deep copy of the slice. +func Copy[T any](a []*T) []*T { + b := make([]*T, len(a)) + for i, p := range a { + if p == nil { + continue + } + v := *p + b[i] = &v + } + return b +} diff --git a/util/slice/slice_test.go b/util/slice/slice_test.go index 5a9b9ec17100f..038813cd578b0 100644 --- a/util/slice/slice_test.go +++ b/util/slice/slice_test.go @@ -43,3 +43,20 @@ func TestSlice(t *testing.T) { }) } } + +func TestCopy(t *testing.T) { + type T int + v0, v1, v2, v3 := T(0), T(1), T(2), T(3) + s := []*T{&v0, &v1, &v2, &v3, nil} + e := Copy(s) + require.Equal(t, len(s), len(e)) + for i := range s { + if s[i] == nil { + require.Nil(t, e[i]) + } else { + require.Equal(t, *s[i], *e[i]) + require.True(t, s[i] != e[i]) + } + } + +}