]> Cypherpunks repositories - gostls13.git/commitdiff
go/build: correct shouldBuild bug reading whole contents of file.
authorRémy Oudompheng <oudomphe@phare.normalesup.org>
Thu, 9 Aug 2012 21:22:51 +0000 (23:22 +0200)
committerRémy Oudompheng <oudomphe@phare.normalesup.org>
Thu, 9 Aug 2012 21:22:51 +0000 (23:22 +0200)
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

src/pkg/go/build/build.go
src/pkg/go/build/build_test.go

index c8a0808efd4dd59c19ccb74e0557106f5c15d636..a65ff437ab347242991e7188793e6cd7cf9c67f5 100644 (file)
@@ -689,7 +689,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
index 560ebad5c97a9cc0296f461d4f35a95bbe5795e0..caa4f26f332e696ceaa78ad523056b6376f1fda2 100644 (file)
@@ -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")
+       }
+}