Skip to content

Commit 94ebb39

Browse files
committed
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+.
1 parent ad97d20 commit 94ebb39

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)