Check ReflectValue.CanAddr before set field value

This commit is contained in:
Jinzhu 2021-04-19 21:32:32 +08:00
parent 15a46bc042
commit d327926425
2 changed files with 6 additions and 1 deletions

View File

@ -36,7 +36,7 @@ var (
// ErrInvalidDB invalid db // ErrInvalidDB invalid db
ErrInvalidDB = errors.New("invalid db") ErrInvalidDB = errors.New("invalid db")
// ErrInvalidValue invalid value // ErrInvalidValue invalid value
ErrInvalidValue = errors.New("invalid value") ErrInvalidValue = errors.New("invalid value, should be pointer to struct or slice")
// ErrInvalidValueOfLength invalid values do not match length // ErrInvalidValueOfLength invalid values do not match length
ErrInvalidValueOfLength = errors.New("invalid association values, length doesn't match") ErrInvalidValueOfLength = errors.New("invalid association values, length doesn't match")
) )

View File

@ -539,6 +539,11 @@ func (stmt *Statement) SetColumn(name string, value interface{}, fromCallbacks .
} }
} }
if !stmt.ReflectValue.CanAddr() {
stmt.AddError(ErrInvalidValue)
return
}
switch stmt.ReflectValue.Kind() { switch stmt.ReflectValue.Kind() {
case reflect.Slice, reflect.Array: case reflect.Slice, reflect.Array:
if len(fromCallbacks) > 0 { if len(fromCallbacks) > 0 {