diff --git a/src/cmd/compile/internal/importer/gcimporter.go b/src/cmd/compile/internal/importer/gcimporter.go index b89d7477c78..47a0d7c0bf3 100644 --- a/src/cmd/compile/internal/importer/gcimporter.go +++ b/src/cmd/compile/internal/importer/gcimporter.go @@ -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: diff --git a/src/cmd/compile/internal/importer/gcimporter_test.go b/src/cmd/compile/internal/importer/gcimporter_test.go index 383d2c9e271..4cf3bee061c 100644 --- a/src/cmd/compile/internal/importer/gcimporter_test.go +++ b/src/cmd/compile/internal/importer/gcimporter_test.go @@ -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) diff --git a/src/cmd/compile/internal/importer/testdata/exports.go b/src/cmd/compile/internal/importer/testdata/exports.go index 91598c03e35..84ba3dfba60 100644 --- a/src/cmd/compile/internal/importer/testdata/exports.go +++ b/src/cmd/compile/internal/importer/testdata/exports.go @@ -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 ( diff --git a/src/go/internal/gcimporter/gcimporter_test.go b/src/go/internal/gcimporter/gcimporter_test.go index 81094fa246a..bfbedf1a7df 100644 --- a/src/go/internal/gcimporter/gcimporter_test.go +++ b/src/go/internal/gcimporter/gcimporter_test.go @@ -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) diff --git a/src/go/internal/gcimporter/testdata/exports.go b/src/go/internal/gcimporter/testdata/exports.go index 3d5a8c9e39f..55fb86001f8 100644 --- a/src/go/internal/gcimporter/testdata/exports.go +++ b/src/go/internal/gcimporter/testdata/exports.go @@ -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 (