Skip to content

Commit fbd90be

Browse files
authored
fix: race condition of Package fields (#1307)
1 parent f0eea95 commit fbd90be

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

lint/package.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ var (
4747

4848
// Files return package's files.
4949
func (p *Package) Files() map[string]*File {
50+
p.RLock()
51+
defer p.RUnlock()
52+
5053
return p.files
5154
}
5255

@@ -74,20 +77,23 @@ func (p *Package) IsMain() bool {
7477
func (p *Package) TypesPkg() *types.Package {
7578
p.RLock()
7679
defer p.RUnlock()
80+
7781
return p.typesPkg
7882
}
7983

8084
// TypesInfo yields type information of this package identifiers
8185
func (p *Package) TypesInfo() *types.Info {
8286
p.RLock()
8387
defer p.RUnlock()
88+
8489
return p.typesInfo
8590
}
8691

8792
// Sortable yields a map of sortable types in this package
8893
func (p *Package) Sortable() map[string]bool {
8994
p.RLock()
9095
defer p.RUnlock()
96+
9197
return p.sortable
9298
}
9399

@@ -150,9 +156,13 @@ func check(config *types.Config, n string, fset *token.FileSet, astFiles []*ast.
150156

151157
// TypeOf returns the type of expression.
152158
func (p *Package) TypeOf(expr ast.Expr) types.Type {
159+
p.RLock()
160+
defer p.RUnlock()
161+
153162
if p.typesInfo == nil {
154163
return nil
155164
}
165+
156166
return p.typesInfo.TypeOf(expr)
157167
}
158168

@@ -166,6 +176,9 @@ const (
166176
)
167177

168178
func (p *Package) scanSortable() {
179+
p.Lock()
180+
defer p.Unlock()
181+
169182
sortableFlags := map[string]sortableMethodsFlags{}
170183
for _, f := range p.files {
171184
for _, decl := range f.AST.Decls {
@@ -191,7 +204,7 @@ func (p *Package) scanSortable() {
191204
func (p *Package) lint(rules []Rule, config Config, failures chan Failure) error {
192205
p.scanSortable()
193206
var eg errgroup.Group
194-
for _, file := range p.files {
207+
for _, file := range p.Files() {
195208
eg.Go(func() error {
196209
return file.lint(rules, config, failures)
197210
})
@@ -202,6 +215,9 @@ func (p *Package) lint(rules []Rule, config Config, failures chan Failure) error
202215

203216
// IsAtLeastGoVersion returns true if the Go version for this package is v or higher, false otherwise
204217
func (p *Package) IsAtLeastGoVersion(v *goversion.Version) bool {
218+
p.RLock()
219+
defer p.RUnlock()
220+
205221
return p.goVersion.GreaterThanOrEqual(v)
206222
}
207223

0 commit comments

Comments
 (0)