diff --git a/modules/indexer/code/indexer.go b/modules/indexer/code/indexer.go index 981167a8254a5..0152212aff445 100644 --- a/modules/indexer/code/indexer.go +++ b/modules/indexer/code/indexer.go @@ -18,6 +18,7 @@ import ( "code.gitea.io/gitea/modules/queue" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/timeutil" + giteautil "code.gitea.io/gitea/modules/util" ) // SearchResult result of performing a search in a repo @@ -185,7 +186,7 @@ func Init() { rIndexer, populate, err = NewBleveIndexer(setting.Indexer.RepoPath) if err != nil { - if rIndexer != nil { + if !giteautil.IsInterfaceNil(rIndexer) { rIndexer.Close() } cancel() @@ -205,7 +206,7 @@ func Init() { rIndexer, populate, err = NewElasticSearchIndexer(setting.Indexer.RepoConnStr, setting.Indexer.RepoIndexerName) if err != nil { - if rIndexer != nil { + if !giteautil.IsInterfaceNil(rIndexer) { rIndexer.Close() } cancel() diff --git a/modules/util/util.go b/modules/util/util.go index cbc6eb4f8a01c..68a2e97423279 100644 --- a/modules/util/util.go +++ b/modules/util/util.go @@ -9,6 +9,7 @@ import ( "crypto/rand" "errors" "math/big" + "reflect" "strconv" "strings" ) @@ -161,3 +162,14 @@ func RandomString(length int64) (string, error) { } return string(bytes), nil } + +// IsInterfaceNil checks if a variable with a typed interface is nil. +// Because interface{} == nil is always false. +func IsInterfaceNil(i interface{}) bool { + switch reflect.TypeOf(i).Kind() { + case reflect.Ptr, reflect.Map, reflect.Array, reflect.Chan, reflect.Slice: + return reflect.ValueOf(i).IsNil() + default: + return false + } +} diff --git a/modules/util/util_test.go b/modules/util/util_test.go index 39cf07c85543e..e0d8439c876e8 100644 --- a/modules/util/util_test.go +++ b/modules/util/util_test.go @@ -169,3 +169,30 @@ func Test_OptionalBool(t *testing.T) { assert.Equal(t, OptionalBoolTrue, OptionalBoolParse("t")) assert.Equal(t, OptionalBoolTrue, OptionalBoolParse("True")) } + +type SampleInterface interface { + Hello() +} + +type BasicInterface int + +func (BasicInterface) Hello() { +} + +func returnFilledBasicInterface() BasicInterface { + return BasicInterface(123123) +} + +func returnNil() *BasicInterface { + return nil +} + +func TestTypedInterfaceNil(t *testing.T) { + var info SampleInterface + + info = returnNil() + assert.Equal(t, IsInterfaceNil(info), true) + + info = returnFilledBasicInterface() + assert.Equal(t, IsInterfaceNil(info), false) +} diff --git a/routers/web/base.go b/routers/web/base.go index 16d3192da21dc..b26104bc9a73e 100644 --- a/routers/web/base.go +++ b/routers/web/base.go @@ -20,6 +20,7 @@ import ( "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/storage" "code.gitea.io/gitea/modules/templates" + giteautil "code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/modules/web/middleware" "code.gitea.io/gitea/services/auth" @@ -130,7 +131,7 @@ func Recovery() func(next http.Handler) http.Handler { log.Error("%v", combinedErr) sessionStore := session.GetSession(req) - if sessionStore == nil { + if giteautil.IsInterfaceNil(sessionStore) { if setting.IsProd { http.Error(w, http.StatusText(500), 500) } else {