Skip to content

Commit c080634

Browse files
committed
fix: stackoverflow
1 parent 86bcf63 commit c080634

File tree

2 files changed

+46
-25
lines changed

2 files changed

+46
-25
lines changed

nilerr.go

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"go/token"
66
"go/types"
7+
"slices"
78

89
"github.com/gostaticanalysis/comment"
910
"github.com/gostaticanalysis/comment/passes/commentmap"
@@ -30,8 +31,10 @@ func run(pass *analysis.Pass) (interface{}, error) {
3031

3132
reportFail := func(v ssa.Value, ret *ssa.Return, format string) {
3233
pos := ret.Pos()
33-
errLines := getValueLineNumbers(pass, v)
3434
if !cmaps.IgnorePos(pos, "nilerr") {
35+
seen := map[string]struct{}{}
36+
errLines := getValueLineNumbers(pass, v, seen)
37+
3538
var errLineText string
3639
if len(errLines) == 1 {
3740
errLineText = fmt.Sprintf("line %d", errLines[0])
@@ -64,12 +67,27 @@ func run(pass *analysis.Pass) (interface{}, error) {
6467
return nil, nil
6568
}
6669

67-
func getValueLineNumbers(pass *analysis.Pass, v ssa.Value) []int {
70+
func getValueLineNumbers(pass *analysis.Pass, v ssa.Value, seen map[string]struct{}) []int {
6871
if phi, ok := v.(*ssa.Phi); ok {
6972
result := make([]int, 0, len(phi.Edges))
73+
7074
for _, edge := range phi.Edges {
71-
result = append(result, getValueLineNumbers(pass, edge)...)
75+
if _, ok := seen[edge.Name()]; ok {
76+
if edge.Pos() == token.NoPos {
77+
continue
78+
}
79+
80+
result = append(result, pass.Fset.File(edge.Pos()).Line(edge.Pos()))
81+
continue
82+
}
83+
84+
seen[edge.Name()] = struct{}{}
85+
86+
result = append(result, getValueLineNumbers(pass, edge, seen)...)
7287
}
88+
89+
slices.Sort(result)
90+
7391
return result
7492
}
7593

@@ -79,6 +97,11 @@ func getValueLineNumbers(pass *analysis.Pass, v ssa.Value) []int {
7997
}
8098

8199
pos := value.Pos()
100+
101+
if pos == token.NoPos {
102+
return nil
103+
}
104+
82105
return []int{pass.Fset.File(pos).Line(pos)}
83106
}
84107

testdata/src/a/a.go

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ func j() (interface{}, error) {
154154
return nil, nil
155155
}
156156

157-
func k() {
157+
func k() {
158158
if err := do(); err != nil {
159159
return
160160
}
@@ -165,31 +165,29 @@ func k() {
165165
}
166166

167167
func l() error {
168-
var e = errors.New("x")
169-
170-
bytes, err := do2()
171-
if err != nil {
172-
return err
168+
aChan := make(chan error, 1)
169+
bChan := make(chan error, 1)
170+
171+
var aErr error
172+
var bErr error
173+
174+
for i := 0; i < 2; i++ {
175+
select {
176+
case err := <-aChan:
177+
aErr = err
178+
case err := <-bChan:
179+
bErr = err
180+
}
173181
}
174-
defer func() error {return nil}()
175182

176-
for {
177-
var buf []byte
178-
buf, err = do2()
179-
if err == e {
180-
for err == e {
181-
_, err = do2()
182-
}
183-
if err != nil {
184-
err = errors.New("a")
185-
break
186-
}
187-
}
188-
_ = buf
183+
if aErr != nil {
184+
return nil // want `error is not nil \(lines \[171 175\]\) but it returns nil`
185+
}
186+
if bErr != nil {
187+
return nil // want `error is not nil \(lines \[172 175\]\) but it returns nil`
189188
}
190189

191-
_ = bytes
192-
return nil // want "error is not nil \\(lines \\[178 181\\]\\) but it returns nil"
190+
return nil
193191
}
194192

195193
func do() error {

0 commit comments

Comments
 (0)