Skip to content

Commit

Permalink
Fix nil checking on typed interface
Browse files Browse the repository at this point in the history
- Partially resoles go-gitea#17596
- Resolves SA4023 errors.
- Ensure correctly that typed interface are nil.
  • Loading branch information
Gusted committed Nov 9, 2021
1 parent a5b4720 commit 6b22b0e
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 3 deletions.
5 changes: 3 additions & 2 deletions modules/indexer/code/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand Down
12 changes: 12 additions & 0 deletions modules/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"crypto/rand"
"errors"
"math/big"
"reflect"
"strconv"
"strings"
)
Expand Down Expand Up @@ -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
}
}
27 changes: 27 additions & 0 deletions modules/util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
3 changes: 2 additions & 1 deletion routers/web/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 6b22b0e

Please sign in to comment.