mirror of https://github.com/golang/go.git
cmd/cover: check that the argument of -var is valid
At the moment, the cover tool does not check that the argument of -var is a valid identifier. Hence, it could generate a file that fails to compile afterwards. Updates #25280 Change-Id: I6eb1872736377680900a18a4a28ba002ab5ea8ca Reviewed-on: https://go-review.googlesource.com/c/120316 Run-TryBot: Ian Lance Taylor <iant@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
This commit is contained in:
parent
1ccb66d1ef
commit
963c9fdf97
|
|
@ -16,6 +16,7 @@ import (
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"sort"
|
"sort"
|
||||||
|
"unicode"
|
||||||
|
|
||||||
"cmd/internal/edit"
|
"cmd/internal/edit"
|
||||||
"cmd/internal/objabi"
|
"cmd/internal/objabi"
|
||||||
|
|
@ -116,6 +117,10 @@ func parseFlags() error {
|
||||||
return fmt.Errorf("too many options")
|
return fmt.Errorf("too many options")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if *varVar != "" && !isValidIdentifier(*varVar) {
|
||||||
|
return fmt.Errorf("argument of -var is not a valid identifier: %v", *varVar)
|
||||||
|
}
|
||||||
|
|
||||||
if *mode != "" {
|
if *mode != "" {
|
||||||
switch *mode {
|
switch *mode {
|
||||||
case "set":
|
case "set":
|
||||||
|
|
@ -676,3 +681,14 @@ func (f *File) addVariables(w io.Writer) {
|
||||||
fmt.Fprintf(w, "var _ = %s.LoadUint32\n", atomicPackageName)
|
fmt.Fprintf(w, "var _ = %s.LoadUint32\n", atomicPackageName)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func isValidIdentifier(ident string) bool {
|
||||||
|
first := true
|
||||||
|
for _, c := range ident {
|
||||||
|
if !unicode.IsLetter(c) && c != '_' && (first || !unicode.IsDigit(c)) {
|
||||||
|
return false // invalid identifier
|
||||||
|
}
|
||||||
|
first = false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -145,6 +145,12 @@ func TestCover(t *testing.T) {
|
||||||
cmd := exec.Command(testcover, "-mode=count", "-var=thisNameMustBeVeryLongToCauseOverflowOfCounterIncrementStatementOntoNextLineForTest", "-o", coverOutput, coverInput)
|
cmd := exec.Command(testcover, "-mode=count", "-var=thisNameMustBeVeryLongToCauseOverflowOfCounterIncrementStatementOntoNextLineForTest", "-o", coverOutput, coverInput)
|
||||||
run(cmd, t)
|
run(cmd, t)
|
||||||
|
|
||||||
|
cmd = exec.Command(testcover, "-mode=set", "-var=Not_an-identifier", "-o", coverOutput, coverInput)
|
||||||
|
err = cmd.Run()
|
||||||
|
if err == nil {
|
||||||
|
t.Error("Expected cover to fail with an error")
|
||||||
|
}
|
||||||
|
|
||||||
// Copy testmain to testTempDir, so that it is in the same directory
|
// Copy testmain to testTempDir, so that it is in the same directory
|
||||||
// as coverOutput.
|
// as coverOutput.
|
||||||
b, err := ioutil.ReadFile(testMain)
|
b, err := ioutil.ReadFile(testMain)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue