Skip to content

Commit 146e8d4

Browse files
nevkontakteianlancetaylor
authored andcommitted
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: 94ebb39 GitHub-Pull-Request: #48346 Reviewed-on: https://go-review.googlesource.com/c/go/+/349469 Reviewed-by: Keith Randall <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Trust: Keith Randall <[email protected]> Run-TryBot: Keith Randall <[email protected]> TryBot-Result: Go Bot <[email protected]>
1 parent 9a58aa2 commit 146e8d4

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed

src/reflect/value.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2940,8 +2940,7 @@ func (v Value) CanConvert(t Type) bool {
29402940
// from slice to pointer-to-array.
29412941
if vt.Kind() == Slice && t.Kind() == Ptr && t.Elem().Kind() == Array {
29422942
n := t.Elem().Len()
2943-
h := (*unsafeheader.Slice)(v.ptr)
2944-
if n > h.Len {
2943+
if n > v.Len() {
29452944
return false
29462945
}
29472946
}
@@ -3208,10 +3207,10 @@ func cvtStringRunes(v Value, t Type) Value {
32083207
// convertOp: []T -> *[N]T
32093208
func cvtSliceArrayPtr(v Value, t Type) Value {
32103209
n := t.Elem().Len()
3211-
h := (*unsafeheader.Slice)(v.ptr)
3212-
if n > h.Len {
3213-
panic("reflect: cannot convert slice with length " + itoa.Itoa(h.Len) + " to pointer to array with length " + itoa.Itoa(n))
3210+
if n > v.Len() {
3211+
panic("reflect: cannot convert slice with length " + itoa.Itoa(v.Len()) + " to pointer to array with length " + itoa.Itoa(n))
32143212
}
3213+
h := (*unsafeheader.Slice)(v.ptr)
32153214
return Value{t.common(), h.Data, v.flag&^(flagIndir|flagAddr|flagKindMask) | flag(Ptr)}
32163215
}
32173216

0 commit comments

Comments
 (0)