Skip to content

Commit da94586

Browse files
ianlancetaylorgopherbot
authored andcommitted
cmd/go: check for errors reading gccgo package list
Previously if there was something invalid about the package list cmd/go would crash rather than reporting a useful error. For #60798 Change-Id: I502facf41442ab49217405b5b1874fff52a6d416 Reviewed-on: https://go-review.googlesource.com/c/go/+/503496 TryBot-Result: Gopher Robot <[email protected]> Run-TryBot: Ian Lance Taylor <[email protected]> Auto-Submit: Ian Lance Taylor <[email protected]> Run-TryBot: Ian Lance Taylor <[email protected]> Reviewed-by: Bryan Mills <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent b01cd41 commit da94586

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

src/cmd/go/internal/work/action.go

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -382,16 +382,23 @@ func (b *Builder) NewObjdir() string {
382382
func readpkglist(shlibpath string) (pkgs []*load.Package) {
383383
var stk load.ImportStack
384384
if cfg.BuildToolchainName == "gccgo" {
385-
f, _ := elf.Open(shlibpath)
385+
f, err := elf.Open(shlibpath)
386+
if err != nil {
387+
base.Fatal(fmt.Errorf("failed to open shared library: %v", err))
388+
}
386389
sect := f.Section(".go_export")
387-
data, _ := sect.Data()
388-
scanner := bufio.NewScanner(bytes.NewBuffer(data))
389-
for scanner.Scan() {
390-
t := scanner.Text()
391-
var found bool
392-
if t, found = strings.CutPrefix(t, "pkgpath "); found {
393-
t = strings.TrimSuffix(t, ";")
394-
pkgs = append(pkgs, load.LoadPackageWithFlags(t, base.Cwd(), &stk, nil, 0))
390+
if sect == nil {
391+
base.Fatal(fmt.Errorf("%s: missing .go_export section", shlibpath))
392+
}
393+
data, err := sect.Data()
394+
if err != nil {
395+
base.Fatal(fmt.Errorf("%s: failed to read .go_export section: %v", shlibpath, err))
396+
}
397+
pkgpath := []byte("pkgpath ")
398+
for _, line := range bytes.Split(data, []byte{'\n'}) {
399+
if path, found := bytes.CutPrefix(line, pkgpath); found {
400+
path = bytes.TrimSuffix(path, []byte{';'})
401+
pkgs = append(pkgs, load.LoadPackageWithFlags(string(path), base.Cwd(), &stk, nil, 0))
395402
}
396403
}
397404
} else {

0 commit comments

Comments
 (0)