diff options
author | Nevkontakte <aleks@nevkontakte.com> | 2021-09-13 22:40:30 +0000 |
---|---|---|
committer | Ian Lance Taylor <iant@golang.org> | 2021-09-14 00:16:36 +0000 |
commit | 146e8d4994052ee4a58bec7e2cf37e568ce1e4e5 (patch) | |
tree | 49e504c4294b48bdf7a6ca0d8c5d205fc89b2e93 | |
parent | 9a58aa267e3686c86d3e5bf1d14117a2a127838c (diff) | |
download | go-146e8d4994052ee4a58bec7e2cf37e568ce1e4e5.tar.gz go-146e8d4994052ee4a58bec7e2cf37e568ce1e4e5.zip |
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+.
Change-Id: I4f113db0c572ec0b81ebfecf5a137145f6c8c41d
GitHub-Last-Rev: 94ebb393bac93579b6455555822691c0d69e2d42
GitHub-Pull-Request: golang/go#48346
Reviewed-on: https://go-review.googlesource.com/c/go/+/349469
Reviewed-by: Keith Randall <khr@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Trust: Keith Randall <khr@golang.org>
Run-TryBot: Keith Randall <khr@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
-rw-r--r-- | src/reflect/value.go | 9 |
1 files changed, 4 insertions, 5 deletions
diff --git a/src/reflect/value.go b/src/reflect/value.go index bc48a76ce6..33b81d7209 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)} } |