Skip to content

Commit 6b89e7d

Browse files
griesemercagedmantis
authored andcommitted
[release-branch.go1.22] cmd/compile: initialize posBaseMap correctly
The posBaseMap is used to identify a file's syntax tree node given a source position. The position is mapped to the file base which is then used to look up the file node in posBaseMap. When posBaseMap is initialized, the file position base is not the file base if there's a line directive before the package clause. This can happen in cgo-generated files, for instance due to an import "C" declaration. If the wrong file position base is used during initialization, looking up a file given a position will not find the file. If a version error occurs and the corresponding file is not found, the old code panicked with a null pointer exception. Make sure to consistently initialize the posBaseMap by factoring out the code computing the file base from a given position. While at it, check for a nil file pointer. This should not happen anymore, but don't crash if it happens (at the cost of a slightly less informative error message). Fixes #67460. Change-Id: I4a6af88699c32ad01fffce124b06bb7f9e06f43d Reviewed-on: https://go-review.googlesource.com/c/go/+/586238 Reviewed-by: Robert Findley <[email protected]> Reviewed-by: Robert Griesemer <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Auto-Submit: Robert Griesemer <[email protected]> Reviewed-on: https://go-review.googlesource.com/c/go/+/586161
1 parent 185457d commit 6b89e7d

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed

src/cmd/compile/internal/noder/irgen.go

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,13 @@ func checkFiles(m posMap, noders []*noder) (*types2.Package, *types2.Info) {
3434
posBaseMap := make(map[*syntax.PosBase]*syntax.File)
3535
for i, p := range noders {
3636
files[i] = p.file
37-
posBaseMap[p.file.Pos().Base()] = p.file
37+
// The file.Pos() is the position of the package clause.
38+
// If there's a //line directive before that, file.Pos().Base()
39+
// refers to that directive, not the file itself.
40+
// Make sure to consistently map back to file base, here and
41+
// when we look for a file in the conf.Error handler below,
42+
// otherwise the file may not be found (was go.dev/issue/67141).
43+
posBaseMap[fileBase(p.file.Pos())] = p.file
3844
}
3945

4046
// typechecking
@@ -68,13 +74,12 @@ func checkFiles(m posMap, noders []*noder) (*types2.Package, *types2.Info) {
6874
terr := err.(types2.Error)
6975
msg := terr.Msg
7076
if versionErrorRx.MatchString(msg) {
71-
posBase := terr.Pos.Base()
72-
for !posBase.IsFileBase() { // line directive base
73-
posBase = posBase.Pos().Base()
74-
}
77+
posBase := fileBase(terr.Pos)
7578
fileVersion := info.FileVersions[posBase]
7679
file := posBaseMap[posBase]
77-
if file.GoVersion == fileVersion {
80+
if file == nil {
81+
// This should never happen, but be careful and don't crash.
82+
} else if file.GoVersion == fileVersion {
7883
// If we have a version error caused by //go:build, report it.
7984
msg = fmt.Sprintf("%s (file declares //go:build %s)", msg, fileVersion)
8085
} else {
@@ -149,6 +154,15 @@ func checkFiles(m posMap, noders []*noder) (*types2.Package, *types2.Info) {
149154
return pkg, info
150155
}
151156

157+
// fileBase returns a file's position base given a position in the file.
158+
func fileBase(pos syntax.Pos) *syntax.PosBase {
159+
base := pos.Base()
160+
for !base.IsFileBase() { // line directive base
161+
base = base.Pos().Base()
162+
}
163+
return base
164+
}
165+
152166
// A cycleFinder detects anonymous interface cycles (go.dev/issue/56103).
153167
type cycleFinder struct {
154168
cyclic map[*types2.Interface]bool

test/fixedbugs/issue67141.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// errorcheck -lang=go1.22
2+
3+
//go:build go1.21
4+
5+
// We need a line directive before the package clause,
6+
// but don't change file name or position so that the
7+
// error message appears at the right place.
8+
9+
//line issue67141.go:10
10+
package p
11+
12+
func _() {
13+
for range 10 { // ERROR "cannot range over 10"
14+
}
15+
}

0 commit comments

Comments
 (0)