std: fix calls to Printf(s) with non-constant s

In all cases the intent was not to interpret s as a format string.
In one case (go/types), this was a latent bug in production.
(These were uncovered by a new check in vet's printf analyzer.)

Updates #60529

Change-Id: I3e17af7e589be9aec1580783a1b1011c52ec494b
Reviewed-on: https://go-review.googlesource.com/c/go/+/587855
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Russ Cox <rsc@golang.org>
This commit is contained in:
Alan Donovan 2024-05-23 10:47:36 -04:00
parent c34c124f40
commit bf91eb3a8b
14 changed files with 18 additions and 18 deletions

View file

@ -533,7 +533,7 @@ func (check *Checker) builtin(x *operand, call *syntax.CallExpr, id builtinId) (
case _Max, _Min: case _Max, _Min:
// max(x, ...) // max(x, ...)
// min(x, ...) // min(x, ...)
check.verifyVersionf(call.Fun, go1_21, quote(bin.name)) check.verifyVersionf(call.Fun, go1_21, "%s", quote(bin.name))
op := token.LSS op := token.LSS
if id == _Max { if id == _Max {

View file

@ -160,7 +160,7 @@ func TestPSSGolden(t *testing.T) {
t.Error(err) t.Error(err)
} }
default: default:
t.Fatalf("unknown marker: " + marker) t.Fatalf("unknown marker: %s", marker)
} }
} }
} }

View file

@ -1753,7 +1753,7 @@ func TestIssue6651(t *testing.T) {
want := "error in rows.Next" want := "error in rows.Next"
rowsCursorNextHook = func(dest []driver.Value) error { rowsCursorNextHook = func(dest []driver.Value) error {
return fmt.Errorf(want) return errors.New(want)
} }
defer func() { rowsCursorNextHook = nil }() defer func() { rowsCursorNextHook = nil }()
@ -1765,7 +1765,7 @@ func TestIssue6651(t *testing.T) {
want = "error in rows.Close" want = "error in rows.Close"
setRowsCloseHook(func(rows *Rows, err *error) { setRowsCloseHook(func(rows *Rows, err *error) {
*err = fmt.Errorf(want) *err = errors.New(want)
}) })
defer setRowsCloseHook(nil) defer setRowsCloseHook(nil)
err = db.QueryRow("SELECT|people|name|").Scan(&v) err = db.QueryRow("SELECT|people|name|").Scan(&v)

View file

@ -1168,7 +1168,7 @@ func TestMarshalUncommonFieldNames(t *testing.T) {
func TestMarshalerError(t *testing.T) { func TestMarshalerError(t *testing.T) {
s := "test variable" s := "test variable"
st := reflect.TypeOf(s) st := reflect.TypeOf(s)
errText := "json: test error" const errText = "json: test error"
tests := []struct { tests := []struct {
CaseName CaseName

View file

@ -536,7 +536,7 @@ func (check *Checker) builtin(x *operand, call *ast.CallExpr, id builtinId) (_ b
case _Max, _Min: case _Max, _Min:
// max(x, ...) // max(x, ...)
// min(x, ...) // min(x, ...)
check.verifyVersionf(call.Fun, go1_21, quote(bin.name)) check.verifyVersionf(call.Fun, go1_21, "%s", quote(bin.name))
op := token.LSS op := token.LSS
if id == _Max { if id == _Max {

View file

@ -488,7 +488,7 @@ func (check *Checker) instantiatedType(ix *typeparams.IndexExpr, def *TypeName)
if i < len(ix.Indices) { if i < len(ix.Indices) {
pos = ix.Indices[i].Pos() pos = ix.Indices[i].Pos()
} }
check.softErrorf(atPos(pos), InvalidTypeArg, err.Error()) check.softErrorf(atPos(pos), InvalidTypeArg, "%v", err)
} else { } else {
check.mono.recordInstance(check.pkg, ix.Pos(), inst.TypeParams().list(), inst.TypeArgs().list(), ix.Indices) check.mono.recordInstance(check.pkg, ix.Pos(), inst.TypeParams().list(), inst.TypeArgs().list(), ix.Indices)
} }

View file

@ -197,7 +197,7 @@ func testForSpecificFunctions(t *testing.T, dir string, want []string, avoid []s
} }
} }
if rval != "" { if rval != "" {
t.Logf("=-= begin output:\n" + output + "\n=-= end output\n") t.Logf("=-= begin output:\n%s\n=-= end output\n", output)
} }
return rval return rval
} }

View file

@ -1909,7 +1909,7 @@ func TestFillBytes(t *testing.T) {
"0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
} { } {
t.Run(n, func(t *testing.T) { t.Run(n, func(t *testing.T) {
t.Logf(n) t.Log(n)
x, ok := new(Int).SetString(n, 0) x, ok := new(Int).SetString(n, 0)
if !ok { if !ok {
panic("invalid test entry") panic("invalid test entry")

View file

@ -77,7 +77,7 @@ func checkSampleDistribution(t *testing.T, samples []float64, expected *statsRes
actual := getStatsResults(samples) actual := getStatsResults(samples)
err := actual.checkSimilarDistribution(expected) err := actual.checkSimilarDistribution(expected)
if err != nil { if err != nil {
t.Errorf(err.Error()) t.Error(err)
} }
} }

View file

@ -74,7 +74,7 @@ func checkSampleDistribution(t *testing.T, samples []float64, expected *statsRes
actual := getStatsResults(samples) actual := getStatsResults(samples)
err := actual.checkSimilarDistribution(expected) err := actual.checkSimilarDistribution(expected)
if err != nil { if err != nil {
t.Errorf(err.Error()) t.Error(err)
} }
} }

View file

@ -4743,11 +4743,11 @@ Host: foo
func TestHandlerFinishSkipBigContentLengthRead(t *testing.T) { func TestHandlerFinishSkipBigContentLengthRead(t *testing.T) {
setParallel(t) setParallel(t)
conn := newTestConn() conn := newTestConn()
conn.readBuf.Write([]byte(fmt.Sprintf( conn.readBuf.WriteString(
"POST / HTTP/1.1\r\n" + "POST / HTTP/1.1\r\n" +
"Host: test\r\n" + "Host: test\r\n" +
"Content-Length: 9999999999\r\n" + "Content-Length: 9999999999\r\n" +
"\r\n" + strings.Repeat("a", 1<<20)))) "\r\n" + strings.Repeat("a", 1<<20))
ls := &oneConnListener{conn} ls := &oneConnListener{conn}
var inHandlerLen int var inHandlerLen int

View file

@ -206,7 +206,7 @@ func (c *Client) Auth(a Auth) error {
} }
resp64 := make([]byte, encoding.EncodedLen(len(resp))) resp64 := make([]byte, encoding.EncodedLen(len(resp)))
encoding.Encode(resp64, resp) encoding.Encode(resp64, resp)
code, msg64, err := c.cmd(0, strings.TrimSpace(fmt.Sprintf("AUTH %s %s", mech, resp64))) code, msg64, err := c.cmd(0, "%s", strings.TrimSpace(fmt.Sprintf("AUTH %s %s", mech, resp64)))
for err == nil { for err == nil {
var msg []byte var msg []byte
switch code { switch code {
@ -232,7 +232,7 @@ func (c *Client) Auth(a Auth) error {
} }
resp64 = make([]byte, encoding.EncodedLen(len(resp))) resp64 = make([]byte, encoding.EncodedLen(len(resp)))
encoding.Encode(resp64, resp) encoding.Encode(resp64, resp)
code, msg64, err = c.cmd(0, string(resp64)) code, msg64, err = c.cmd(0, "%s", resp64)
} }
return err return err
} }

View file

@ -772,10 +772,10 @@ func TestSendMail(t *testing.T) {
tc := textproto.NewConn(conn) tc := textproto.NewConn(conn)
for i := 0; i < len(data) && data[i] != ""; i++ { for i := 0; i < len(data) && data[i] != ""; i++ {
tc.PrintfLine(data[i]) tc.PrintfLine("%s", data[i])
for len(data[i]) >= 4 && data[i][3] == '-' { for len(data[i]) >= 4 && data[i][3] == '-' {
i++ i++
tc.PrintfLine(data[i]) tc.PrintfLine("%s", data[i])
} }
if data[i] == "221 Goodbye" { if data[i] == "221 Goodbye" {
return return

View file

@ -1272,7 +1272,7 @@ func TestMutexProfile(t *testing.T) {
if ok, err := regexp.MatchString(r3, lines[5]); err != nil || !ok { if ok, err := regexp.MatchString(r3, lines[5]); err != nil || !ok {
t.Errorf("%q didn't match %q", lines[5], r3) t.Errorf("%q didn't match %q", lines[5], r3)
} }
t.Logf(prof) t.Log(prof)
}) })
t.Run("proto", func(t *testing.T) { t.Run("proto", func(t *testing.T) {
// proto format // proto format