From 94ebb393bac93579b6455555822691c0d69e2d42 Mon Sep 17 00:00:00 2001 From: Nevkontakte Date: Sun, 12 Sep 2021 18:57:58 +0100 Subject: [PATCH] reflect: Use Value.Len instead of conversion to slice header. This change is functionally equivalent, but reduces reliance on unsafe features. This would allow GopherJS to avoid an additional patch to the standard library we'd have to maintain in order to remain compatible with Go 1.17+. --- src/reflect/value.go | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/reflect/value.go b/src/reflect/value.go index 6e9aaabe8a1298..ee0b5de66ebba6 100644 --- a/src/reflect/value.go +++ b/src/reflect/value.go @@ -2940,8 +2940,7 @@ func (v Value) CanConvert(t Type) bool { // from slice to pointer-to-array. if vt.Kind() == Slice && t.Kind() == Ptr && t.Elem().Kind() == Array { n := t.Elem().Len() - h := (*unsafeheader.Slice)(v.ptr) - if n > h.Len { + if n > v.Len() { return false } } @@ -3208,10 +3207,10 @@ func cvtStringRunes(v Value, t Type) Value { // convertOp: []T -> *[N]T func cvtSliceArrayPtr(v Value, t Type) Value { n := t.Elem().Len() - h := (*unsafeheader.Slice)(v.ptr) - if n > h.Len { - panic("reflect: cannot convert slice with length " + itoa.Itoa(h.Len) + " to pointer to array with length " + itoa.Itoa(n)) + if n > v.Len() { + panic("reflect: cannot convert slice with length " + itoa.Itoa(v.Len()) + " to pointer to array with length " + itoa.Itoa(n)) } + h := (*unsafeheader.Slice)(v.ptr) return Value{t.common(), h.Data, v.flag&^(flagIndir|flagAddr|flagKindMask) | flag(Ptr)} }