Skip to content

Commit 9960b69

Browse files
committed
gopls/internal/lsp/cache: guard against "unsafe" by package path, not ID
The crash in golang/go#60890 suggests that a user encountered a variant of the unsafe package. I'm not sure how to reproduce this, but in any case we should be checking package path, not ID, when guarding against exporting "unsafe". For golang/go#60890 Change-Id: Ib6c546b8f74ba513f5ee3df09b5ba29cea0c1b85 Reviewed-on: https://go-review.googlesource.com/c/tools/+/504555 Run-TryBot: Robert Findley <[email protected]> TryBot-Result: Gopher Robot <[email protected]> gopls-CI: kokoro <[email protected]> Reviewed-by: Alan Donovan <[email protected]>
1 parent 6480332 commit 9960b69

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

gopls/internal/lsp/cache/check.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,13 @@ func (b *typeCheckBatch) getImportPackage(ctx context.Context, id PackageID) (pk
453453
}
454454

455455
ph := b.handles[id]
456+
457+
// Do a second check for "unsafe" defensively, due to golang/go#60890.
458+
if ph.m.PkgPath == "unsafe" {
459+
bug.Reportf("encountered \"unsafe\" as %s (golang/go#60890)", id)
460+
return types.Unsafe, nil
461+
}
462+
456463
data, err := filecache.Get(exportDataKind, ph.key)
457464
if err == filecache.ErrNotFound {
458465
// No cached export data: type-check as fast as possible.
@@ -632,13 +639,17 @@ func (b *typeCheckBatch) checkPackage(ctx context.Context, ph *packageHandle) (*
632639
diagnosticsKind: encodeDiagnostics(pkg.diagnostics),
633640
}
634641

635-
if ph.m.ID != "unsafe" { // unsafe cannot be exported
642+
if ph.m.PkgPath != "unsafe" { // unsafe cannot be exported
636643
exportData, err := gcimporter.IExportShallow(pkg.fset, pkg.types)
637644
if err != nil {
638645
bug.Reportf("exporting package %v: %v", ph.m.ID, err)
639646
} else {
640647
toCache[exportDataKind] = exportData
641648
}
649+
} else if ph.m.ID != "unsafe" {
650+
// golang/go#60890: we should only ever see one variant of the "unsafe"
651+
// package.
652+
bug.Reportf("encountered \"unsafe\" as %s (golang/go#60890)", ph.m.ID)
642653
}
643654

644655
for kind, data := range toCache {

0 commit comments

Comments
 (0)