1
0
mirror of https://github.com/golang/go synced 2024-11-22 05:14:40 -07:00

cmd/compile/internal/importer: exportdata section ends with the last index of "\n$$\n"

This fixes a bug in the test only function Import where it looked for
the first instance of the string "\n$$\n" as the end of the exportdata
section. This should look for the last instance of "\n$$\n" within
the ar file.

Adds unit tests that demonstrate the error.

Added comments to tests that can correctly use the first instance.

Change-Id: I7a85afa41cf1c2902119516b757b7c6625d46d13
Reviewed-on: https://go-review.googlesource.com/c/go/+/626775
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Robert Griesemer <gri@google.com>
This commit is contained in:
Tim King 2024-11-08 14:00:49 -08:00
parent 1f8fa4941f
commit 0ff1d42507
5 changed files with 8 additions and 2 deletions

View File

@ -240,8 +240,8 @@ func Import(packages map[string]*types2.Package, path, srcDir string, lookup fun
// appropriate importer.
switch exportFormat {
case 'u':
// TODO(taking): Look into whether this should be LastIndex instead of Index.
s = s[:strings.Index(s, "\n$$\n")]
// exported strings may contain "\n$$\n" - search backwards
s = s[:strings.LastIndex(s, "\n$$\n")]
input := pkgbits.NewPkgDecoder(id, s)
pkg = ReadPackage(nil, packages, input)
default:

View File

@ -204,6 +204,8 @@ func TestVersionHandling(t *testing.T) {
}
// 2) find export data
i := bytes.Index(data, []byte("\n$$B\n")) + 5
// Export data can contain "\n$$\n" in string constants, however,
// searching for the next end of section marker "\n$$\n" is good enough for testzs.
j := bytes.Index(data[i:], []byte("\n$$\n")) + i
if i < 0 || j < 0 || i > j {
t.Fatalf("export data section not found (i = %d, j = %d)", i, j)

View File

@ -26,6 +26,7 @@ const (
C8 = 42
C9 int = 42
C10 float64 = 42
C11 = "\n$$\n" // an object file export data marker - export data extraction must not be led astray
)
type (

View File

@ -329,6 +329,8 @@ func TestVersionHandling(t *testing.T) {
}
// 2) find export data
i := bytes.Index(data, []byte("\n$$B\n")) + 5
// Export data can contain "\n$$\n" in string constants, however,
// searching for the next end of section marker "\n$$\n" is good enough for tests.
j := bytes.Index(data[i:], []byte("\n$$\n")) + i
if i < 0 || j < 0 || i > j {
t.Fatalf("export data section not found (i = %d, j = %d)", i, j)

View File

@ -26,6 +26,7 @@ const (
C8 = 42
C9 int = 42
C10 float64 = 42
C11 = "\n$$\n" // an object file export data marker - export data extraction must not be led astray
)
type (