mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-15 13:02:14 +08:00
refactor: error if walking too deep on form mapping
Signed-off-by: thxCode <thxcode0824@gmail.com>
This commit is contained in:
parent
ea03e10384
commit
466e46f65f
@ -17,7 +17,8 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
errUnknownType = errors.New("unknown type")
|
||||
errUnknownType = errors.New("unknown type")
|
||||
errRecursionTooDeep = errors.New("recursion too deep")
|
||||
|
||||
// ErrConvertMapStringSlice can not convert to map[string][]string
|
||||
ErrConvertMapStringSlice = errors.New("can not convert to map slices of strings")
|
||||
@ -74,14 +75,18 @@ func (form formSource) TrySet(value reflect.Value, field reflect.StructField, ta
|
||||
}
|
||||
|
||||
func mappingByPtr(ptr any, setter setter, tag string) error {
|
||||
_, err := mapping(reflect.ValueOf(ptr), emptyField, setter, tag)
|
||||
_, err := mapping(reflect.ValueOf(ptr), emptyField, setter, tag, 0)
|
||||
return err
|
||||
}
|
||||
|
||||
func mapping(value reflect.Value, field reflect.StructField, setter setter, tag string) (bool, error) {
|
||||
func mapping(value reflect.Value, field reflect.StructField, setter setter, tag string, deepth int) (bool, error) {
|
||||
if field.Tag.Get(tag) == "-" { // just ignoring this field
|
||||
return false, nil
|
||||
}
|
||||
if deepth >= 1000 {
|
||||
// avoid causing stackoverflow.
|
||||
return false, errRecursionTooDeep
|
||||
}
|
||||
|
||||
vKind := value.Kind()
|
||||
|
||||
@ -92,7 +97,7 @@ func mapping(value reflect.Value, field reflect.StructField, setter setter, tag
|
||||
isNew = true
|
||||
vPtr = reflect.New(value.Type().Elem())
|
||||
}
|
||||
isSet, err := mapping(vPtr.Elem(), field, setter, tag)
|
||||
isSet, err := mapping(vPtr.Elem(), field, setter, tag, deepth+1)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
@ -121,7 +126,7 @@ func mapping(value reflect.Value, field reflect.StructField, setter setter, tag
|
||||
if sf.PkgPath != "" && !sf.Anonymous { // unexported
|
||||
continue
|
||||
}
|
||||
ok, err := mapping(value.Field(i), sf, setter, tag)
|
||||
ok, err := mapping(value.Field(i), sf, setter, tag, deepth+1)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
@ -52,7 +52,7 @@ func TestMappingBaseTypes(t *testing.T) {
|
||||
|
||||
field := val.Elem().Type().Field(0)
|
||||
|
||||
_, err := mapping(val, emptyField, formSource{field.Name: {tt.form}}, "form")
|
||||
_, err := mapping(val, emptyField, formSource{field.Name: {tt.form}}, "form", 0)
|
||||
assert.NoError(t, err, testName)
|
||||
|
||||
actual := val.Elem().Field(0).Interface()
|
||||
@ -279,6 +279,16 @@ func TestMappingMapField(t *testing.T) {
|
||||
assert.Equal(t, map[string]int{"one": 1}, s.M)
|
||||
}
|
||||
|
||||
func TestMappingCircularRef(t *testing.T) {
|
||||
type S struct {
|
||||
S *S `form:"s"`
|
||||
}
|
||||
var s S
|
||||
|
||||
err := mappingByPtr(&s, formSource{}, "form")
|
||||
assert.ErrorIs(t, err, errRecursionTooDeep)
|
||||
}
|
||||
|
||||
func TestMappingIgnoredCircularRef(t *testing.T) {
|
||||
type S struct {
|
||||
S *S `form:"-"`
|
||||
|
Loading…
x
Reference in New Issue
Block a user