Skip to content

Commit

Permalink
nuke reflect changes minus list.List
Browse files Browse the repository at this point in the history
  • Loading branch information
deniseli committed Aug 19, 2024
1 parent 92588fa commit 061a410
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 45 deletions.
23 changes: 3 additions & 20 deletions internal/reflect/reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,7 @@ func copyAny(src any, ptrs map[uintptr]any, copyConf *copyConfig) (dst any) {
reflect.Complex64, reflect.Complex128, reflect.Func:
dst = copyPremitive(src, ptrs, copyConf)
case reflect.String:
if v.Type() == reflect.TypeFor[string]() {
dst = strings.Clone(src.(string))
} else {
dst = copyStringAlias(src, ptrs, copyConf)
}
dst = strings.Clone(src.(string))
case reflect.Slice:
dst = copySlice(src, ptrs, copyConf)
case reflect.Array:
Expand Down Expand Up @@ -192,13 +188,6 @@ func copyPremitive(src any, ptr map[uintptr]any, copyConf *copyConfig) (dst any)
return
}

func copyStringAlias(src any, ptr map[uintptr]any, copyConf *copyConfig) any {
v := reflect.ValueOf(src)
dc := reflect.New(v.Type()).Elem()
dc.Set(v)
return dc.Interface()
}

func copySlice(x any, ptrs map[uintptr]any, copyConf *copyConfig) any {
v := reflect.ValueOf(x)
kind := v.Kind()
Expand Down Expand Up @@ -253,14 +242,8 @@ func copyPointer(x any, ptrs map[uintptr]any, copyConf *copyConfig) any {
v := reflect.ValueOf(x)
t := reflect.TypeOf(x)

if v.Kind() != reflect.Pointer && v.Kind() != reflect.UnsafePointer {
panic(fmt.Errorf("reflect: internal error: must be a Pointer or UnsafePointer; got %v", v.Kind()))
}

if v.Kind() == reflect.UnsafePointer {
// For unsafe.Pointer, just return a copy of the pointer itself, since it
// has no element type to copy (t.Elem() panics).
return x
if v.Kind() != reflect.Pointer {
panic(fmt.Errorf("reflect: internal error: must be a Pointer; got %v", v.Kind()))
}

if v.IsNil() {
Expand Down
25 changes: 0 additions & 25 deletions internal/reflect/reflect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,10 @@ package reflect
import (
"container/list"
"testing"
"unsafe"

"gotest.tools/v3/assert"
)

type mystring string
type structWithMystring struct {
str mystring
}

func TestAliasedString(t *testing.T) {
output := DeepCopy(structWithMystring{"asdf"})
assert.Equal(t, output, structWithMystring{"asdf"})
}

func TestListElements(t *testing.T) {
l := list.New()
l.PushBack("one")
Expand All @@ -26,20 +15,6 @@ func TestListElements(t *testing.T) {
assert.Equal(t, output.Len(), l.Len())
}

type structWithUnsafePtr struct {
ptr unsafe.Pointer
}

func TestUnsafePointer(t *testing.T) {
var x int
// #nosec G103
obj := structWithUnsafePtr{ptr: unsafe.Pointer(&x)}
DeepCopy(obj) // should not panic
x = 3
intPtr := (*int)(obj.ptr)
assert.Equal(t, 3, *intPtr)
}

type structOfPointers struct {
intPtr *int
floatPtr *float64
Expand Down

0 comments on commit 061a410

Please sign in to comment.