]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: check for errors reading gccgo package list
authorIan Lance Taylor <iant@golang.org>
Wed, 14 Jun 2023 21:26:59 +0000 (14:26 -0700)
committerGopher Robot <gobot@golang.org>
Wed, 14 Jun 2023 22:09:07 +0000 (22:09 +0000)
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 <gobot@golang.org>
Run-TryBot: Ian Lance Taylor <iant@google.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Bryan Mills <bcmills@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
src/cmd/go/internal/work/action.go

index 0fc85da0066415cd2df9c29895b3bccab2362da5..d4d0a71e750e7173c2e86f7de4b65616ea41a97b 100644 (file)
@@ -382,16 +382,23 @@ func (b *Builder) NewObjdir() string {
 func readpkglist(shlibpath string) (pkgs []*load.Package) {
        var stk load.ImportStack
        if cfg.BuildToolchainName == "gccgo" {
-               f, _ := elf.Open(shlibpath)
+               f, err := elf.Open(shlibpath)
+               if err != nil {
+                       base.Fatal(fmt.Errorf("failed to open shared library: %v", err))
+               }
                sect := f.Section(".go_export")
-               data, _ := sect.Data()
-               scanner := bufio.NewScanner(bytes.NewBuffer(data))
-               for scanner.Scan() {
-                       t := scanner.Text()
-                       var found bool
-                       if t, found = strings.CutPrefix(t, "pkgpath "); found {
-                               t = strings.TrimSuffix(t, ";")
-                               pkgs = append(pkgs, load.LoadPackageWithFlags(t, base.Cwd(), &stk, nil, 0))
+               if sect == nil {
+                       base.Fatal(fmt.Errorf("%s: missing .go_export section", shlibpath))
+               }
+               data, err := sect.Data()
+               if err != nil {
+                       base.Fatal(fmt.Errorf("%s: failed to read .go_export section: %v", shlibpath, err))
+               }
+               pkgpath := []byte("pkgpath ")
+               for _, line := range bytes.Split(data, []byte{'\n'}) {
+                       if path, found := bytes.CutPrefix(line, pkgpath); found {
+                               path = bytes.TrimSuffix(path, []byte{';'})
+                               pkgs = append(pkgs, load.LoadPackageWithFlags(string(path), base.Cwd(), &stk, nil, 0))
                        }
                }
        } else {