]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile/internal/importer: exportdata section ends with the last index of "\n...
authorTim King <taking@google.com>
Fri, 8 Nov 2024 22:00:49 +0000 (14:00 -0800)
committerTim King <taking@google.com>
Wed, 13 Nov 2024 00:03:12 +0000 (00:03 +0000)
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>
src/cmd/compile/internal/importer/gcimporter.go
src/cmd/compile/internal/importer/gcimporter_test.go
src/cmd/compile/internal/importer/testdata/exports.go
src/go/internal/gcimporter/gcimporter_test.go
src/go/internal/gcimporter/testdata/exports.go

index b89d7477c78a6431109a141cf4febdbca9edcff3..47a0d7c0bf31ec0a800eb6ff4ad453e267187ccb 100644 (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:
index 383d2c9e271e300924ea4c2f0e696cb3f6c560a3..4cf3bee061c87740ccf15d2bb4f94fbf8d17ebe3 100644 (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)
index 91598c03e35c0337edd1f51b4b399135b32be8dd..84ba3dfba609c76aba9880e735e39657402994a7 100644 (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 (
index 81094fa246a81f260f04e8b291828a77e7b4df54..bfbedf1a7df6bb43a94b211fb785e2f2ca4068ed 100644 (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)
index 3d5a8c9e39fe13507ae3061f4bf778337c825fb5..55fb86001f87701274a75a0c48a942313c0d242e 100644 (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 (