fix panic when convert sql and args with nil time.Time pointer (#2038)

this pr fix a panic, when using nil time.Time pointer for input.
not sure if there are others similar code.
please review it, thanks!

Co-authored-by: finelog <kaicltw@gmail.com>
Reviewed-on: https://gitea.com/xorm/xorm/pulls/2038
Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com>
Co-authored-by: finelog <finelog@noreply.gitea.io>
Co-committed-by: finelog <finelog@noreply.gitea.io>
This commit is contained in:
finelog 2021-08-26 00:03:18 +08:00 committed by Lunny Xiao
parent 0a429a241d
commit e5c89cf55e
2 changed files with 19 additions and 2 deletions

View File

@ -962,9 +962,9 @@ func (statement *Statement) convertSQLOrArgs(sqlOrArgs ...interface{}) (string,
if len(sqlOrArgs) > 1 { if len(sqlOrArgs) > 1 {
var newArgs = make([]interface{}, 0, len(sqlOrArgs)-1) var newArgs = make([]interface{}, 0, len(sqlOrArgs)-1)
for _, arg := range sqlOrArgs[1:] { for _, arg := range sqlOrArgs[1:] {
if v, ok := arg.(*time.Time); ok { if v, ok := arg.(time.Time); ok {
newArgs = append(newArgs, v.In(statement.defaultTimeZone).Format("2006-01-02 15:04:05")) newArgs = append(newArgs, v.In(statement.defaultTimeZone).Format("2006-01-02 15:04:05"))
} else if v, ok := arg.(time.Time); ok { } else if v, ok := arg.(*time.Time); ok && v != nil {
newArgs = append(newArgs, v.In(statement.defaultTimeZone).Format("2006-01-02 15:04:05")) newArgs = append(newArgs, v.In(statement.defaultTimeZone).Format("2006-01-02 15:04:05"))
} else { } else {
newArgs = append(newArgs, arg) newArgs = append(newArgs, arg)

View File

@ -77,6 +77,23 @@ func TestColumnsStringGeneration(t *testing.T) {
} }
} }
func TestConvertSQLOrArgs(t *testing.T) {
statement, err := createTestStatement()
assert.NoError(t, err)
// example orm struct
// type Table struct {
// ID int
// del *time.Time `xorm:"deleted"`
// }
args := []interface{}{
"INSERT `table` (`id`, `del`) VALUES (?, ?)", 1, (*time.Time)(nil),
}
// before fix, here will panic
_, _, err = statement.convertSQLOrArgs(args...)
assert.NoError(t, err)
}
func BenchmarkGetFlagForColumnWithICKey_ContainsKey(b *testing.B) { func BenchmarkGetFlagForColumnWithICKey_ContainsKey(b *testing.B) {
b.StopTimer() b.StopTimer()