Skip to content

Commit 6b22b0e

Browse files
author
Gusted
committed
Fix nil checking on typed interface
- Partially resoles go-gitea#17596 - Resolves SA4023 errors. - Ensure correctly that typed interface are nil.
1 parent a5b4720 commit 6b22b0e

File tree

4 files changed

+44
-3
lines changed

4 files changed

+44
-3
lines changed

modules/indexer/code/indexer.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"code.gitea.io/gitea/modules/queue"
1919
"code.gitea.io/gitea/modules/setting"
2020
"code.gitea.io/gitea/modules/timeutil"
21+
giteautil "code.gitea.io/gitea/modules/util"
2122
)
2223

2324
// SearchResult result of performing a search in a repo
@@ -185,7 +186,7 @@ func Init() {
185186

186187
rIndexer, populate, err = NewBleveIndexer(setting.Indexer.RepoPath)
187188
if err != nil {
188-
if rIndexer != nil {
189+
if !giteautil.IsInterfaceNil(rIndexer) {
189190
rIndexer.Close()
190191
}
191192
cancel()
@@ -205,7 +206,7 @@ func Init() {
205206

206207
rIndexer, populate, err = NewElasticSearchIndexer(setting.Indexer.RepoConnStr, setting.Indexer.RepoIndexerName)
207208
if err != nil {
208-
if rIndexer != nil {
209+
if !giteautil.IsInterfaceNil(rIndexer) {
209210
rIndexer.Close()
210211
}
211212
cancel()

modules/util/util.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"crypto/rand"
1010
"errors"
1111
"math/big"
12+
"reflect"
1213
"strconv"
1314
"strings"
1415
)
@@ -161,3 +162,14 @@ func RandomString(length int64) (string, error) {
161162
}
162163
return string(bytes), nil
163164
}
165+
166+
// IsInterfaceNil checks if a variable with a typed interface is nil.
167+
// Because interface{} == nil is always false.
168+
func IsInterfaceNil(i interface{}) bool {
169+
switch reflect.TypeOf(i).Kind() {
170+
case reflect.Ptr, reflect.Map, reflect.Array, reflect.Chan, reflect.Slice:
171+
return reflect.ValueOf(i).IsNil()
172+
default:
173+
return false
174+
}
175+
}

modules/util/util_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,3 +169,30 @@ func Test_OptionalBool(t *testing.T) {
169169
assert.Equal(t, OptionalBoolTrue, OptionalBoolParse("t"))
170170
assert.Equal(t, OptionalBoolTrue, OptionalBoolParse("True"))
171171
}
172+
173+
type SampleInterface interface {
174+
Hello()
175+
}
176+
177+
type BasicInterface int
178+
179+
func (BasicInterface) Hello() {
180+
}
181+
182+
func returnFilledBasicInterface() BasicInterface {
183+
return BasicInterface(123123)
184+
}
185+
186+
func returnNil() *BasicInterface {
187+
return nil
188+
}
189+
190+
func TestTypedInterfaceNil(t *testing.T) {
191+
var info SampleInterface
192+
193+
info = returnNil()
194+
assert.Equal(t, IsInterfaceNil(info), true)
195+
196+
info = returnFilledBasicInterface()
197+
assert.Equal(t, IsInterfaceNil(info), false)
198+
}

routers/web/base.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"code.gitea.io/gitea/modules/setting"
2121
"code.gitea.io/gitea/modules/storage"
2222
"code.gitea.io/gitea/modules/templates"
23+
giteautil "code.gitea.io/gitea/modules/util"
2324
"code.gitea.io/gitea/modules/web/middleware"
2425
"code.gitea.io/gitea/services/auth"
2526

@@ -130,7 +131,7 @@ func Recovery() func(next http.Handler) http.Handler {
130131
log.Error("%v", combinedErr)
131132

132133
sessionStore := session.GetSession(req)
133-
if sessionStore == nil {
134+
if giteautil.IsInterfaceNil(sessionStore) {
134135
if setting.IsProd {
135136
http.Error(w, http.StatusText(500), 500)
136137
} else {

0 commit comments

Comments
 (0)