diff options
author | Daniel Martà <mvdan@mvdan.cc> | 2018-08-16 14:39:13 +0100 |
---|---|---|
committer | Russ Cox <rsc@golang.org> | 2018-08-17 15:51:47 +0000 |
commit | 2482451f76f47707e29b19cafdcaf754badd024b (patch) | |
tree | 85ccd4f00bc359e64373ca392175cce1dd9f265b | |
parent | 876c6d1f274a3d508fa97558ca6765cf324c2939 (diff) | |
download | go-2482451f76f47707e29b19cafdcaf754badd024b.tar.gz go-2482451f76f47707e29b19cafdcaf754badd024b.zip |
cmd/vet: don't suggest ... if it breaks a program
It is possible to write a function that seems to wrap a print/printf
call, but then doesn't. For example, if the string parameter we thought
was the format is used as another argument.
One option would be to make vet's print analysis smarter, to detect when
format strings are indeed used like we initially suspected.
However, I've opted for a simpler solution - check if the print/printf
call is already using more than one variadic argument, in which case
using an ellipsis in the last one would break the program:
// too many arguments in call to fmt.Printf
fmt.Printf(format, arg0, args...)
Fixes #26979.
Change-Id: I39371f1cec8483cfd2770a91670c1e80cbb9efdf
Reviewed-on: https://go-review.googlesource.com/129575
Run-TryBot: Daniel Martà <mvdan@mvdan.cc>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Russ Cox <rsc@golang.org>
-rw-r--r-- | src/cmd/vet/print.go | 14 | ||||
-rw-r--r-- | src/cmd/vet/testdata/print.go | 4 |
2 files changed, 18 insertions, 0 deletions
diff --git a/src/cmd/vet/print.go b/src/cmd/vet/print.go index 90fd4ed379..a55da1d3c8 100644 --- a/src/cmd/vet/print.go +++ b/src/cmd/vet/print.go @@ -259,6 +259,20 @@ func checkPrintfFwd(pkg *Package, w *printfWrapper, call *ast.CallExpr, kind int } if !call.Ellipsis.IsValid() { + typ, ok := pkg.types[call.Fun].Type.(*types.Signature) + if !ok { + return + } + if len(call.Args) > typ.Params().Len() { + // If we're passing more arguments than what the + // print/printf function can take, adding an ellipsis + // would break the program. For example: + // + // func foo(arg1 string, arg2 ...interface{} { + // fmt.Printf("%s %v", arg1, arg2) + // } + return + } if !vcfg.VetxOnly { desc := "printf" if kind == kindPrint { diff --git a/src/cmd/vet/testdata/print.go b/src/cmd/vet/testdata/print.go index 7c0cbcf05a..ecafed5fa2 100644 --- a/src/cmd/vet/testdata/print.go +++ b/src/cmd/vet/testdata/print.go @@ -446,6 +446,10 @@ func (*ptrStringer) BadWrapf(x int, format string, args ...interface{}) string { return fmt.Sprintf(format, args) // ERROR "missing ... in args forwarded to printf-like function" } +func (*ptrStringer) WrapfFalsePositive(x int, arg1 string, arg2 ...interface{}) string { + return fmt.Sprintf("%s %v", arg1, arg2) +} + type embeddedStringer struct { foo string ptrStringer |