mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
reflect: fix StructOf panics from too many methods in embedded fields
Previously we panicked if the number of methods present for an embedded
field was >= 32. This change removes that limit and now StructOf
dynamically calls itself to create space for the number of methods.
Fixes #25402
Change-Id: I3b1deb119796d25f7e6eee1cdb126327b49a0b5e
GitHub-Last-Rev: 16da71ad6b
GitHub-Pull-Request: golang/go#26865
Reviewed-on: https://go-review.googlesource.com/c/128479
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
This commit is contained in:
parent
510eea2dfc
commit
0e4a0b93d2
2 changed files with 30 additions and 60 deletions
|
|
@ -5019,6 +5019,17 @@ func TestStructOfWithInterface(t *testing.T) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestStructOfTooManyFields(t *testing.T) {
|
||||||
|
// Bug Fix: #25402 - this should not panic
|
||||||
|
tt := StructOf([]StructField{
|
||||||
|
{Name: "Time", Type: TypeOf(time.Time{}), Anonymous: true},
|
||||||
|
})
|
||||||
|
|
||||||
|
if _, present := tt.MethodByName("After"); !present {
|
||||||
|
t.Errorf("Expected method `After` to be found")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestChanOf(t *testing.T) {
|
func TestChanOf(t *testing.T) {
|
||||||
// check construction and use of type not in binary
|
// check construction and use of type not in binary
|
||||||
type T string
|
type T string
|
||||||
|
|
|
||||||
|
|
@ -1889,6 +1889,8 @@ func MapOf(key, elem Type) Type {
|
||||||
return ti.(Type)
|
return ti.(Type)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO(crawshaw): as these funcTypeFixedN structs have no methods,
|
||||||
|
// they could be defined at runtime using the StructOf function.
|
||||||
type funcTypeFixed4 struct {
|
type funcTypeFixed4 struct {
|
||||||
funcType
|
funcType
|
||||||
args [4]*rtype
|
args [4]*rtype
|
||||||
|
|
@ -2278,42 +2280,6 @@ type structTypeUncommon struct {
|
||||||
u uncommonType
|
u uncommonType
|
||||||
}
|
}
|
||||||
|
|
||||||
// A *rtype representing a struct is followed directly in memory by an
|
|
||||||
// array of method objects representing the methods attached to the
|
|
||||||
// struct. To get the same layout for a run time generated type, we
|
|
||||||
// need an array directly following the uncommonType memory. The types
|
|
||||||
// structTypeFixed4, ...structTypeFixedN are used to do this.
|
|
||||||
//
|
|
||||||
// A similar strategy is used for funcTypeFixed4, ...funcTypeFixedN.
|
|
||||||
|
|
||||||
// TODO(crawshaw): as these structTypeFixedN and funcTypeFixedN structs
|
|
||||||
// have no methods, they could be defined at runtime using the StructOf
|
|
||||||
// function.
|
|
||||||
|
|
||||||
type structTypeFixed4 struct {
|
|
||||||
structType
|
|
||||||
u uncommonType
|
|
||||||
m [4]method
|
|
||||||
}
|
|
||||||
|
|
||||||
type structTypeFixed8 struct {
|
|
||||||
structType
|
|
||||||
u uncommonType
|
|
||||||
m [8]method
|
|
||||||
}
|
|
||||||
|
|
||||||
type structTypeFixed16 struct {
|
|
||||||
structType
|
|
||||||
u uncommonType
|
|
||||||
m [16]method
|
|
||||||
}
|
|
||||||
|
|
||||||
type structTypeFixed32 struct {
|
|
||||||
structType
|
|
||||||
u uncommonType
|
|
||||||
m [32]method
|
|
||||||
}
|
|
||||||
|
|
||||||
// isLetter reports whether a given 'rune' is classified as a Letter.
|
// isLetter reports whether a given 'rune' is classified as a Letter.
|
||||||
func isLetter(ch rune) bool {
|
func isLetter(ch rune) bool {
|
||||||
return 'a' <= ch && ch <= 'z' || 'A' <= ch && ch <= 'Z' || ch == '_' || ch >= utf8.RuneSelf && unicode.IsLetter(ch)
|
return 'a' <= ch && ch <= 'z' || 'A' <= ch && ch <= 'Z' || ch == '_' || ch >= utf8.RuneSelf && unicode.IsLetter(ch)
|
||||||
|
|
@ -2571,33 +2537,26 @@ func StructOf(fields []StructField) Type {
|
||||||
var typ *structType
|
var typ *structType
|
||||||
var ut *uncommonType
|
var ut *uncommonType
|
||||||
|
|
||||||
switch {
|
if len(methods) == 0 {
|
||||||
case len(methods) == 0:
|
|
||||||
t := new(structTypeUncommon)
|
t := new(structTypeUncommon)
|
||||||
typ = &t.structType
|
typ = &t.structType
|
||||||
ut = &t.u
|
ut = &t.u
|
||||||
case len(methods) <= 4:
|
} else {
|
||||||
t := new(structTypeFixed4)
|
// A *rtype representing a struct is followed directly in memory by an
|
||||||
typ = &t.structType
|
// array of method objects representing the methods attached to the
|
||||||
ut = &t.u
|
// struct. To get the same layout for a run time generated type, we
|
||||||
copy(t.m[:], methods)
|
// need an array directly following the uncommonType memory.
|
||||||
case len(methods) <= 8:
|
// A similar strategy is used for funcTypeFixed4, ...funcTypeFixedN.
|
||||||
t := new(structTypeFixed8)
|
tt := New(StructOf([]StructField{
|
||||||
typ = &t.structType
|
{Name: "S", Type: TypeOf(structType{})},
|
||||||
ut = &t.u
|
{Name: "U", Type: TypeOf(uncommonType{})},
|
||||||
copy(t.m[:], methods)
|
{Name: "M", Type: ArrayOf(len(methods), TypeOf(methods[0]))},
|
||||||
case len(methods) <= 16:
|
}))
|
||||||
t := new(structTypeFixed16)
|
|
||||||
typ = &t.structType
|
typ = (*structType)(unsafe.Pointer(tt.Elem().Field(0).UnsafeAddr()))
|
||||||
ut = &t.u
|
ut = (*uncommonType)(unsafe.Pointer(tt.Elem().Field(1).UnsafeAddr()))
|
||||||
copy(t.m[:], methods)
|
|
||||||
case len(methods) <= 32:
|
copy(tt.Elem().Field(2).Slice(0, len(methods)).Interface().([]method), methods)
|
||||||
t := new(structTypeFixed32)
|
|
||||||
typ = &t.structType
|
|
||||||
ut = &t.u
|
|
||||||
copy(t.m[:], methods)
|
|
||||||
default:
|
|
||||||
panic("reflect.StructOf: too many methods")
|
|
||||||
}
|
}
|
||||||
// TODO(sbinet): Once we allow embedding multiple types,
|
// TODO(sbinet): Once we allow embedding multiple types,
|
||||||
// methods will need to be sorted like the compiler does.
|
// methods will need to be sorted like the compiler does.
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue