Skip to content

Commit

Permalink
internal/gcimporter: update FindExportData to return a non-negative size
Browse files Browse the repository at this point in the history
Updates FindExportData to return a non-negative size when there is no
error. This can only happen if the archive file is malformed.

Updates golang/go#70651

Change-Id: Ia422651cd31fba83a260cf1be600ffd134012a01
Reviewed-on: https://go-review.googlesource.com/c/tools/+/633278
Reviewed-by: Alan Donovan <adonovan@google.com>
Reviewed-by: Robert Griesemer <gri@google.com>
Reviewed-by: Robert Findley <rfindley@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Commit-Queue: Tim King <taking@google.com>
  • Loading branch information
timothy-king authored and Go LUCI committed Dec 3, 2024
1 parent 25b0003 commit 399ee16
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 15 deletions.
20 changes: 7 additions & 13 deletions go/gcexportdata/gcexportdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,19 +111,13 @@ func NewReader(r io.Reader) (io.Reader, error) {
return nil, err
}

if size >= 0 {
// We were given an archive and found the __.PKGDEF in it.
// This tells us the size of the export data, and we don't
// need to return the entire file.
return &io.LimitedReader{
R: buf,
N: size,
}, nil
} else {
// We were given an object file. As such, we don't know how large
// the export data is and must return the entire file.
return buf, nil
}
// We were given an archive and found the __.PKGDEF in it.
// This tells us the size of the export data, and we don't
// need to return the entire file.
return &io.LimitedReader{
R: buf,
N: size,
}, nil
}

// readAll works the same way as io.ReadAll, but avoids allocations and copies
Expand Down
8 changes: 6 additions & 2 deletions internal/gcimporter/exportdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func readGopackHeader(r *bufio.Reader) (name string, size int64, err error) {
// file by reading from it. The reader must be positioned at the
// start of the file before calling this function. The hdr result
// is the string before the export data, either "$$" or "$$B".
// The size result is the length of the export data in bytes, or -1 if not known.
// The size result is the length of the export data in bytes.
//
// This function is needed by [gcexportdata.Read], which must
// accept inputs produced by the last two releases of cmd/compile,
Expand All @@ -67,6 +67,7 @@ func FindExportData(r *bufio.Reader) (hdr string, size int64, err error) {
if name, size, err = readGopackHeader(r); err != nil {
return
}
arsize := size

// First entry should be __.PKGDEF.
if name != "__.PKGDEF" {
Expand Down Expand Up @@ -99,8 +100,11 @@ func FindExportData(r *bufio.Reader) (hdr string, size int64, err error) {
size -= int64(len(line))
}
hdr = string(line)
// TODO(taking): Return an error when hdr != "$$B\n".
// TODO(taking): Remove end-of-section marker "\n$$\n" from size.

if size < 0 {
size = -1
err = fmt.Errorf("invalid size (%d) in the archive file: %d bytes remain without section headers (recompile package)", arsize, size)
}

return
Expand Down

0 comments on commit 399ee16

Please sign in to comment.