]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile/internal/noder,go/internal/gcimporter: return an error if not an archive...
authorTim King <taking@google.com>
Fri, 8 Nov 2024 19:49:00 +0000 (11:49 -0800)
committerGo LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Tue, 12 Nov 2024 19:51:37 +0000 (19:51 +0000)
Return an error from FindExportData variants if the contents are not
an archive file.

Change-Id: I2fa8d3553638ef1de6a03e2ce46341f00ed6965f
Reviewed-on: https://go-review.googlesource.com/c/go/+/626697
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Robert Griesemer <gri@google.com>
Commit-Queue: Tim King <taking@google.com>

src/cmd/compile/internal/importer/exportdata.go
src/cmd/compile/internal/importer/gcimporter_test.go
src/cmd/compile/internal/noder/import.go
src/go/internal/gcimporter/exportdata.go
src/go/internal/gcimporter/gcimporter_test.go

index c7fe63c8f1582fddecf59e570a9b696153393cb2..2ae8c1b4d96da43776ec2516a1563330d98af307 100644 (file)
@@ -58,30 +58,32 @@ func FindExportData(r *bufio.Reader) (hdr string, size int, err error) {
                return
        }
 
-       if string(line) == "!<arch>\n" {
-               // Archive file. Scan to __.PKGDEF.
-               var name string
-               if name, size, err = readGopackHeader(r); err != nil {
-                       return
-               }
+       // Is the first line an archive file signature?
+       if string(line) != "!<arch>\n" {
+               err = fmt.Errorf("not the start of an archive file (%q)", line)
+               return
+       }
 
-               // First entry should be __.PKGDEF.
-               if name != "__.PKGDEF" {
-                       err = fmt.Errorf("go archive is missing __.PKGDEF")
-                       return
-               }
+       // Archive file. Scan to __.PKGDEF.
+       var name string
+       if name, size, err = readGopackHeader(r); err != nil {
+               return
+       }
 
-               // Read first line of __.PKGDEF data, so that line
-               // is once again the first line of the input.
-               if line, err = r.ReadSlice('\n'); err != nil {
-                       err = fmt.Errorf("can't find export data (%v)", err)
-                       return
-               }
+       // First entry should be __.PKGDEF.
+       if name != "__.PKGDEF" {
+               err = fmt.Errorf("go archive is missing __.PKGDEF")
+               return
+       }
+
+       // Read first line of __.PKGDEF data, so that line
+       // is once again the first line of the input.
+       if line, err = r.ReadSlice('\n'); err != nil {
+               err = fmt.Errorf("can't find export data (%v)", err)
+               return
        }
-       // TODO(taking): The else case is likely dead. Otherwise, size<0. Return an error instead.
 
-       // Now at __.PKGDEF in archive or still at beginning of file.
-       // Either way, line should begin with "go object ".
+       // Now at __.PKGDEF in archive. line should begin with "go object ".
        if !strings.HasPrefix(string(line), "go object ") {
                err = fmt.Errorf("not a Go object file")
                return
index a202ee10dec2bb019dd09cc12ded2fc3d55ab9e3..383d2c9e271e300924ea4c2f0e696cb3f6c560a3 100644 (file)
@@ -162,17 +162,29 @@ func TestVersionHandling(t *testing.T) {
                // test that export data can be imported
                _, err := Import(make(map[string]*types2.Package), pkgpath, dir, nil)
                if err != nil {
-                       // ok to fail if it fails with a no longer supported error for select files
+                       // ok to fail if it fails with a 'not the start of an archive file' error for select files
                        if strings.Contains(err.Error(), "no longer supported") {
                                switch name {
-                               case "test_go1.7_0.a", "test_go1.7_1.a",
-                                       "test_go1.8_4.a", "test_go1.8_5.a",
-                                       "test_go1.11_6b.a", "test_go1.11_999b.a":
+                               case "test_go1.8_4.a",
+                                       "test_go1.8_5.a":
                                        continue
                                }
                                // fall through
                        }
-                       // ok to fail if it fails with a newer version error for select files
+                       // ok to fail if it fails with a 'no longer supported' error for select files
+                       if strings.Contains(err.Error(), "no longer supported") {
+                               switch name {
+                               case "test_go1.7_0.a",
+                                       "test_go1.7_1.a",
+                                       "test_go1.8_4.a",
+                                       "test_go1.8_5.a",
+                                       "test_go1.11_6b.a",
+                                       "test_go1.11_999b.a":
+                                       continue
+                               }
+                               // fall through
+                       }
+                       // ok to fail if it fails with a 'newer version' error for select files
                        if strings.Contains(err.Error(), "newer version") {
                                switch name {
                                case "test_go1.11_999i.a":
index 1e4c1ecb63cc499b0e8d93c13605317e24b27a0b..964b01ec42438095c48f889b37e5eed3ea64e444 100644 (file)
@@ -266,27 +266,22 @@ func findExportData(f *os.File) (r *bio.Reader, end int64, err error) {
                return
        }
 
-       if line == "!<arch>\n" { // package archive
-               // package export block should be first
-               sz := int64(archive.ReadHeader(r.Reader, "__.PKGDEF"))
-               if sz <= 0 {
-                       err = errors.New("not a package file")
-                       return
-               }
-               end = r.Offset() + sz
-               line, err = r.ReadString('\n')
-               if err != nil {
-                       return
-               }
-       } else {
-               // Not an archive; provide end of file instead.
-               // TODO(mdempsky): I don't think this happens anymore.
-               var fi os.FileInfo
-               fi, err = f.Stat()
-               if err != nil {
-                       return
-               }
-               end = fi.Size()
+       // Is the first line an archive file signature?
+       if line != "!<arch>\n" {
+               err = fmt.Errorf("not the start of an archive file (%q)", line)
+               return
+       }
+
+       // package export block should be first
+       sz := int64(archive.ReadHeader(r.Reader, "__.PKGDEF"))
+       if sz <= 0 {
+               err = errors.New("not a package file")
+               return
+       }
+       end = r.Offset() + sz
+       line, err = r.ReadString('\n')
+       if err != nil {
+               return
        }
 
        if !strings.HasPrefix(line, "go object ") {
index 4aa22d7c923969877ffd7b0af91795725a6636d5..ec17c1dd1a9b02647a0c81eaf82c125aa60b0940 100644 (file)
@@ -48,29 +48,32 @@ func FindExportData(r *bufio.Reader) (hdr string, size int, err error) {
                return
        }
 
-       if string(line) == "!<arch>\n" {
-               // Archive file. Scan to __.PKGDEF.
-               var name string
-               if name, size, err = readGopackHeader(r); err != nil {
-                       return
-               }
+       // Is the first line an archive file signature?
+       if string(line) != "!<arch>\n" {
+               err = fmt.Errorf("not the start of an archive file (%q)", line)
+               return
+       }
 
-               // First entry should be __.PKGDEF.
-               if name != "__.PKGDEF" {
-                       err = fmt.Errorf("go archive is missing __.PKGDEF")
-                       return
-               }
+       // Archive file. Scan to __.PKGDEF.
+       var name string
+       if name, size, err = readGopackHeader(r); err != nil {
+               return
+       }
 
-               // Read first line of __.PKGDEF data, so that line
-               // is once again the first line of the input.
-               if line, err = r.ReadSlice('\n'); err != nil {
-                       err = fmt.Errorf("can't find export data (%v)", err)
-                       return
-               }
+       // First entry should be __.PKGDEF.
+       if name != "__.PKGDEF" {
+               err = fmt.Errorf("go archive is missing __.PKGDEF")
+               return
+       }
+
+       // Read first line of __.PKGDEF data, so that line
+       // is once again the first line of the input.
+       if line, err = r.ReadSlice('\n'); err != nil {
+               err = fmt.Errorf("can't find export data (%v)", err)
+               return
        }
 
-       // Now at __.PKGDEF in archive or still at beginning of file.
-       // Either way, line should begin with "go object ".
+       // Now at __.PKGDEF in archive. line should begin with "go object ".
        if !strings.HasPrefix(string(line), "go object ") {
                err = fmt.Errorf("not a Go object file")
                return
index 11bd22d71760e90ddff7b1aeb829989b87afc2dd..81094fa246a81f260f04e8b291828a77e7b4df54 100644 (file)
@@ -285,7 +285,16 @@ func TestVersionHandling(t *testing.T) {
                // test that export data can be imported
                _, err := Import(fset, make(map[string]*types.Package), pkgpath, dir, nil)
                if err != nil {
-                       // ok to fail if it fails with a no longer supported error for select files
+                       // ok to fail if it fails with a 'not the start of an archive file' error for select files
+                       if strings.Contains(err.Error(), "not the start of an archive file") {
+                               switch name {
+                               case "test_go1.8_4.a",
+                                       "test_go1.8_5.a":
+                                       continue
+                               }
+                               // fall through
+                       }
+                       // ok to fail if it fails with a 'no longer supported' error for select files
                        if strings.Contains(err.Error(), "no longer supported") {
                                switch name {
                                case "test_go1.7_0.a",
@@ -300,7 +309,7 @@ func TestVersionHandling(t *testing.T) {
                                }
                                // fall through
                        }
-                       // ok to fail if it fails with a newer version error for select files
+                       // ok to fail if it fails with a 'newer version' error for select files
                        if strings.Contains(err.Error(), "newer version") {
                                switch name {
                                case "test_go1.11_999i.a":