Skip to content

Commit c2df0ef

Browse files
timothy-kingGo LUCI
authored and
Go LUCI
committed
internal/gcimporter: remove end-of-section marker from size
Remove the bytes for the end-of-section marker "\n$$\n" from the end of the exportdata by descreasing the returned size appropriately. This marker is not a part of the export data so it should be removed. Updates the unified IR reader to not expect the marker to be present. Updates golang/go#70651 Change-Id: Id3324ff21162d80f8e3d1c9792e9157b01ae1a3f Reviewed-on: https://go-review.googlesource.com/c/tools/+/633296 Reviewed-by: Robert Findley <[email protected]> Commit-Queue: Tim King <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent ecb0abc commit c2df0ef

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

internal/gcimporter/exportdata.go

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func readGopackHeader(r *bufio.Reader) (name string, size int64, err error) {
4242
// export data section of an underlying cmd/compile created archive
4343
// file by reading from it. The reader must be positioned at the
4444
// start of the file before calling this function.
45-
// The size result is the length of the export data in bytes.
45+
// This returns the length of the export data in bytes.
4646
//
4747
// This function is needed by [gcexportdata.Read], which must
4848
// accept inputs produced by the last two releases of cmd/compile,
@@ -62,11 +62,12 @@ func FindExportData(r *bufio.Reader) (size int64, err error) {
6262
}
6363

6464
// Archive file. Scan to __.PKGDEF.
65+
var arsize int64
6566
var name string
66-
if name, size, err = readGopackHeader(r); err != nil {
67+
if name, arsize, err = readGopackHeader(r); err != nil {
6768
return
6869
}
69-
arsize := size
70+
size = arsize
7071

7172
// First entry should be __.PKGDEF.
7273
if name != "__.PKGDEF" {
@@ -105,7 +106,21 @@ func FindExportData(r *bufio.Reader) (size int64, err error) {
105106
err = fmt.Errorf("unknown export data header: %q", hdr)
106107
return
107108
}
108-
// TODO(taking): Remove end-of-section marker "\n$$\n" from size.
109+
110+
// For files with a binary export data header "$$B\n",
111+
// these are always terminated by an end-of-section marker "\n$$\n".
112+
// So the last bytes must always be this constant.
113+
//
114+
// The end-of-section marker is not a part of the export data itself.
115+
// Do not include these in size.
116+
//
117+
// It would be nice to have sanity check that the final bytes after
118+
// the export data are indeed the end-of-section marker. The split
119+
// of gcexportdata.NewReader and gcexportdata.Read make checking this
120+
// ugly so gcimporter gives up enforcing this. The compiler and go/types
121+
// importer do enforce this, which seems good enough.
122+
const endofsection = "\n$$\n"
123+
size -= int64(len(endofsection))
109124

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

internal/gcimporter/gcimporter_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,7 @@ func TestVersionHandling(t *testing.T) {
327327
t.Fatal(err)
328328
}
329329
// 2) find export data
330+
// Index is an incorrect but 'good enough for tests' way to find the end of the export data.
330331
i := bytes.Index(data, []byte("\n$$B\n")) + 5
331332
j := bytes.Index(data[i:], []byte("\n$$\n")) + i
332333
if i < 0 || j < 0 || i > j {

internal/gcimporter/ureader_yes.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"go/token"
1212
"go/types"
1313
"sort"
14-
"strings"
1514

1615
"golang.org/x/tools/internal/aliases"
1716
"golang.org/x/tools/internal/pkgbits"
@@ -71,7 +70,6 @@ func UImportData(fset *token.FileSet, imports map[string]*types.Package, data []
7170
}
7271

7372
s := string(data)
74-
s = s[:strings.LastIndex(s, "\n$$\n")]
7573
input := pkgbits.NewPkgDecoder(path, s)
7674
pkg = readUnifiedPackage(fset, nil, imports, input)
7775
return

0 commit comments

Comments
 (0)