From: Hiroshi Ioka Date: Tue, 19 Dec 2017 14:59:56 +0000 (+0900) Subject: cmd/buildid: fix rewrite algorithm X-Git-Tag: go1.10beta2~60 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=afd090c0c07b86de1cdeb2415d3fc187709832ac;p=gostls13.git cmd/buildid: fix rewrite algorithm Update rewrite algorithm by coping code from go/internal/work/buildid:updateBuildID. Probably, this is not the best option. We could provide high-level API in cmd/internal/buildid in the future. Fixes #23181 Change-Id: I336a7c50426ab39bc9998b55c372af61a4fb21a7 Reviewed-on: https://go-review.googlesource.com/84735 Reviewed-by: Russ Cox --- diff --git a/src/cmd/buildid/buildid.go b/src/cmd/buildid/buildid.go index 8d810ffdd9..1c7b228c98 100644 --- a/src/cmd/buildid/buildid.go +++ b/src/cmd/buildid/buildid.go @@ -22,6 +22,21 @@ func usage() { var wflag = flag.Bool("w", false, "write build ID") +// taken from cmd/go/internal/work/buildid.go +func hashToString(h [32]byte) string { + const b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_" + const chunks = 5 + var dst [chunks * 4]byte + for i := 0; i < chunks; i++ { + v := uint32(h[3*i])<<16 | uint32(h[3*i+1])<<8 | uint32(h[3*i+2]) + dst[4*i+0] = b64[(v>>18)&0x3F] + dst[4*i+1] = b64[(v>>12)&0x3F] + dst[4*i+2] = b64[(v>>6)&0x3F] + dst[4*i+3] = b64[v&0x3F] + } + return string(dst[:]) +} + func main() { log.SetPrefix("buildid: ") log.SetFlags(0) @@ -41,6 +56,8 @@ func main() { return } + // Keep in sync with src/cmd/go/internal/work/buildid.go:updateBuildID + f, err := os.Open(file) if err != nil { log.Fatal(err) @@ -51,14 +68,14 @@ func main() { } f.Close() - tail := id - if i := strings.LastIndex(id, "."); i >= 0 { - tail = tail[i+1:] + newID := id[:strings.LastIndex(id, "/")] + "/" + hashToString(hash) + if len(newID) != len(id) { + log.Fatalf("%s: build ID length mismatch %q vs %q", file, id, newID) } - if len(tail) != len(hash)*2 { - log.Fatalf("%s: cannot find %d-byte hash in id %s", file, len(hash), id) + + if len(matches) == 0 { + return } - newID := id[:len(id)-len(tail)] + fmt.Sprintf("%x", hash) f, err = os.OpenFile(file, os.O_WRONLY, 0) if err != nil { diff --git a/src/cmd/go/go_test.go b/src/cmd/go/go_test.go index b3fb5dce4f..18e5eae2bc 100644 --- a/src/cmd/go/go_test.go +++ b/src/cmd/go/go_test.go @@ -4773,12 +4773,14 @@ func TestExecBuildX(t *testing.T) { tg := testgo(t) defer tg.cleanup() + tg.setenv("GOCACHE", "off") + tg.tempFile("main.go", `package main; import "C"; func main() { print("hello") }`) src := tg.path("main.go") obj := tg.path("main") tg.run("build", "-x", "-o", obj, src) sh := tg.path("test.sh") - err := ioutil.WriteFile(sh, []byte(tg.getStderr()), 0666) + err := ioutil.WriteFile(sh, []byte("set -e\n"+tg.getStderr()), 0666) if err != nil { t.Fatal(err) } diff --git a/src/cmd/go/internal/work/buildid.go b/src/cmd/go/internal/work/buildid.go index 3c90c15a70..c685263141 100644 --- a/src/cmd/go/internal/work/buildid.go +++ b/src/cmd/go/internal/work/buildid.go @@ -408,6 +408,8 @@ func (b *Builder) flushOutput(a *Action) { // a.buildID to record as the build ID in the resulting package or binary. // updateBuildID computes the final content ID and updates the build IDs // in the binary. +// +// Keep in sync with src/cmd/buildid/buildid.go func (b *Builder) updateBuildID(a *Action, target string, rewrite bool) error { if cfg.BuildX || cfg.BuildN { if rewrite {