aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRémy Oudompheng <oudomphe@phare.normalesup.org>2012-09-22 05:54:35 +1000
committerRémy Oudompheng <oudomphe@phare.normalesup.org>2012-09-22 05:54:35 +1000
commitbe1207e73511a93f9ae9b9321afc4ef8ee20692a (patch)
treeb1c6fac2a369b28f2a8891ab0d9a3ab12be00d8a
parentab4821d468aa6b8d22e5ef935aa4945d4203a5f1 (diff)
downloadgo-be1207e73511a93f9ae9b9321afc4ef8ee20692a.tar.gz
go-be1207e73511a93f9ae9b9321afc4ef8ee20692a.zip
[release-branch.go1] go/build: correct shouldBuild bug reading whole contents of file.
««« backport 307fafbc2d6f go/build: correct shouldBuild bug reading whole contents of file. It was caused by bytes.TrimSpace being able to return a nil slice. Fixes #3914. R=golang-dev, r CC=golang-dev, remy https://golang.org/cl/6458091 »»»
-rw-r--r--src/pkg/go/build/build.go2
-rw-r--r--src/pkg/go/build/build_test.go29
2 files changed, 30 insertions, 1 deletions
diff --git a/src/pkg/go/build/build.go b/src/pkg/go/build/build.go
index c1145fa726..67e73c5e4a 100644
--- a/src/pkg/go/build/build.go
+++ b/src/pkg/go/build/build.go
@@ -678,7 +678,7 @@ func (ctxt *Context) shouldBuild(content []byte) bool {
}
line = bytes.TrimSpace(line)
if len(line) == 0 { // Blank line
- end = cap(content) - cap(line) // &line[0] - &content[0]
+ end = len(content) - len(p)
continue
}
if !bytes.HasPrefix(line, slashslash) { // Not comment line
diff --git a/src/pkg/go/build/build_test.go b/src/pkg/go/build/build_test.go
index 560ebad5c9..caa4f26f33 100644
--- a/src/pkg/go/build/build_test.go
+++ b/src/pkg/go/build/build_test.go
@@ -75,3 +75,32 @@ func TestLocalDirectory(t *testing.T) {
t.Fatalf("ImportPath=%q, want %q", p.ImportPath, "go/build")
}
}
+
+func TestShouldBuild(t *testing.T) {
+ const file1 = "// +build tag1\n\n" +
+ "package main\n"
+
+ const file2 = "// +build cgo\n\n" +
+ "// This package implements parsing of tags like\n" +
+ "// +build tag1\n" +
+ "package build"
+
+ const file3 = "// Copyright The Go Authors.\n\n" +
+ "package build\n\n" +
+ "// shouldBuild checks tags given by lines of the form\n" +
+ "// +build tag\n" +
+ "func shouldBuild(content []byte)\n"
+
+ ctx := &Context{BuildTags: []string{"tag1"}}
+ if !ctx.shouldBuild([]byte(file1)) {
+ t.Errorf("should not build file1, expected the contrary")
+ }
+ if ctx.shouldBuild([]byte(file2)) {
+ t.Errorf("should build file2, expected the contrary")
+ }
+
+ ctx = &Context{BuildTags: nil}
+ if !ctx.shouldBuild([]byte(file3)) {
+ t.Errorf("should not build file3, expected the contrary")
+ }
+}