From d327926425afecbe084997ba195497107cd71a92 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Mon, 19 Apr 2021 21:32:32 +0800 Subject: [PATCH] Check ReflectValue.CanAddr before set field value --- errors.go | 2 +- statement.go | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/errors.go b/errors.go index 569207a6..f1f6c137 100644 --- a/errors.go +++ b/errors.go @@ -36,7 +36,7 @@ var ( // ErrInvalidDB invalid db ErrInvalidDB = errors.New("invalid db") // 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 = errors.New("invalid association values, length doesn't match") ) diff --git a/statement.go b/statement.go index 32bc462a..2734752d 100644 --- a/statement.go +++ b/statement.go @@ -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() { case reflect.Slice, reflect.Array: if len(fromCallbacks) > 0 {