]> Cypherpunks repositories - gostls13.git/commitdiff
std: fix calls to Printf(s) with non-constant s
authorAlan Donovan <adonovan@google.com>
Thu, 23 May 2024 14:47:36 +0000 (10:47 -0400)
committerAlan Donovan <adonovan@google.com>
Thu, 23 May 2024 18:42:28 +0000 (18:42 +0000)
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>
14 files changed:
src/cmd/compile/internal/types2/builtins.go
src/crypto/rsa/pss_test.go
src/database/sql/sql_test.go
src/encoding/json/encode_test.go
src/go/types/builtins.go
src/go/types/typexpr.go
src/internal/coverage/cfile/emitdata_test.go
src/math/big/int_test.go
src/math/rand/rand_test.go
src/math/rand/v2/rand_test.go
src/net/http/serve_test.go
src/net/smtp/smtp.go
src/net/smtp/smtp_test.go
src/runtime/pprof/pprof_test.go

index 8b08e498f3e70bfc5e216833503c1eb56943bb04..d176cf0967c9f6cbca51c92f8027a66fa7e3dfc3 100644 (file)
@@ -533,7 +533,7 @@ func (check *Checker) builtin(x *operand, call *syntax.CallExpr, id builtinId) (
        case _Max, _Min:
                // max(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
                if id == _Max {
index cf03e3cb7ed2cc903bdecec91795a224eabed4b7..7e908d4389d506e92763df8711586ce8ba916df2 100644 (file)
@@ -160,7 +160,7 @@ func TestPSSGolden(t *testing.T) {
                                t.Error(err)
                        }
                default:
-                       t.Fatalf("unknown marker: " + marker)
+                       t.Fatalf("unknown marker: %s", marker)
                }
        }
 }
index 7dfc6434e049f3d20e55f325b4709cb4b74f74ef..ff65e877a5af6badf038350c34cc47ce6fadfea0 100644 (file)
@@ -1753,7 +1753,7 @@ func TestIssue6651(t *testing.T) {
 
        want := "error in rows.Next"
        rowsCursorNextHook = func(dest []driver.Value) error {
-               return fmt.Errorf(want)
+               return errors.New(want)
        }
        defer func() { rowsCursorNextHook = nil }()
 
@@ -1765,7 +1765,7 @@ func TestIssue6651(t *testing.T) {
 
        want = "error in rows.Close"
        setRowsCloseHook(func(rows *Rows, err *error) {
-               *err = fmt.Errorf(want)
+               *err = errors.New(want)
        })
        defer setRowsCloseHook(nil)
        err = db.QueryRow("SELECT|people|name|").Scan(&v)
index 53259f4a9b78d77aeeaeed01742fc48ab2167bbc..23a14d0b1729279b3b657f23b5cd88ee542f90f3 100644 (file)
@@ -1168,7 +1168,7 @@ func TestMarshalUncommonFieldNames(t *testing.T) {
 func TestMarshalerError(t *testing.T) {
        s := "test variable"
        st := reflect.TypeOf(s)
-       errText := "json: test error"
+       const errText = "json: test error"
 
        tests := []struct {
                CaseName
index b8963f32481b0580a8926ddcac91822b263937e5..4761fbd6ef4bd79777eef3f024553d8412b4380a 100644 (file)
@@ -536,7 +536,7 @@ func (check *Checker) builtin(x *operand, call *ast.CallExpr, id builtinId) (_ b
        case _Max, _Min:
                // max(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
                if id == _Max {
index b31f8b33f66beeeed6208b8fe7fac6450c129fa2..dea6d3199753c1a2d97e6ce4bce25a1ccf1ce834 100644 (file)
@@ -488,7 +488,7 @@ func (check *Checker) instantiatedType(ix *typeparams.IndexExpr, def *TypeName)
                                if i < len(ix.Indices) {
                                        pos = ix.Indices[i].Pos()
                                }
-                               check.softErrorf(atPos(pos), InvalidTypeArg, err.Error())
+                               check.softErrorf(atPos(pos), InvalidTypeArg, "%v", err)
                        } else {
                                check.mono.recordInstance(check.pkg, ix.Pos(), inst.TypeParams().list(), inst.TypeArgs().list(), ix.Indices)
                        }
index a6f2d99a173d273a6af411922519d89d98806123..c522048173e4c7a54d2691fc0f1ac0e00678dc57 100644 (file)
@@ -197,7 +197,7 @@ func testForSpecificFunctions(t *testing.T, dir string, want []string, avoid []s
                }
        }
        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
 }
index 088bce09f9169ca8bb84e9ffcbf54642a902f384..f701652f1b96897163682475f982430d6fc1d824 100644 (file)
@@ -1909,7 +1909,7 @@ func TestFillBytes(t *testing.T) {
                "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
        } {
                t.Run(n, func(t *testing.T) {
-                       t.Logf(n)
+                       t.Log(n)
                        x, ok := new(Int).SetString(n, 0)
                        if !ok {
                                panic("invalid test entry")
index 016cc699201c903314050c853d92fdd00a2c9610..7906f296743fe6252cf563c0108e175302bed9b7 100644 (file)
@@ -77,7 +77,7 @@ func checkSampleDistribution(t *testing.T, samples []float64, expected *statsRes
        actual := getStatsResults(samples)
        err := actual.checkSimilarDistribution(expected)
        if err != nil {
-               t.Errorf(err.Error())
+               t.Error(err)
        }
 }
 
index e89ee29f6006065f05a164319237c7c602241af7..65049cf44de383d5fdbf2ed0b8902c74a4a18b9c 100644 (file)
@@ -74,7 +74,7 @@ func checkSampleDistribution(t *testing.T, samples []float64, expected *statsRes
        actual := getStatsResults(samples)
        err := actual.checkSimilarDistribution(expected)
        if err != nil {
-               t.Errorf(err.Error())
+               t.Error(err)
        }
 }
 
index 5014c24969e4d58781ec0796b210cd83c48e1242..34b7d57f4014072e3853926a30d3fbdc9a74ca7c 100644 (file)
@@ -4743,11 +4743,11 @@ Host: foo
 func TestHandlerFinishSkipBigContentLengthRead(t *testing.T) {
        setParallel(t)
        conn := newTestConn()
-       conn.readBuf.Write([]byte(fmt.Sprintf(
+       conn.readBuf.WriteString(
                "POST / HTTP/1.1\r\n" +
                        "Host: test\r\n" +
                        "Content-Length: 9999999999\r\n" +
-                       "\r\n" + strings.Repeat("a", 1<<20))))
+                       "\r\n" + strings.Repeat("a", 1<<20))
 
        ls := &oneConnListener{conn}
        var inHandlerLen int
index b7877936da57dd40295ef327dd67dc975f463ff2..d750a2854cbf90bbfd62047a82dcd1caaf678965 100644 (file)
@@ -206,7 +206,7 @@ func (c *Client) Auth(a Auth) error {
        }
        resp64 := make([]byte, encoding.EncodedLen(len(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 {
                var msg []byte
                switch code {
@@ -232,7 +232,7 @@ func (c *Client) Auth(a Auth) error {
                }
                resp64 = make([]byte, encoding.EncodedLen(len(resp)))
                encoding.Encode(resp64, resp)
-               code, msg64, err = c.cmd(0, string(resp64))
+               code, msg64, err = c.cmd(0, "%s", resp64)
        }
        return err
 }
index 259b10b93d9e36a7bc8d1fa060bd01aea1834d02..c91c99b1f5311193c39360da6beda953269a5878 100644 (file)
@@ -772,10 +772,10 @@ func TestSendMail(t *testing.T) {
 
                tc := textproto.NewConn(conn)
                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] == '-' {
                                i++
-                               tc.PrintfLine(data[i])
+                               tc.PrintfLine("%s", data[i])
                        }
                        if data[i] == "221 Goodbye" {
                                return
index 512e07e491bb4d4027e0d9030d9154b278bf3328..09abbb31ae6ccd02a31df2e62edc250da8ec62db 100644 (file)
@@ -1272,7 +1272,7 @@ func TestMutexProfile(t *testing.T) {
                if ok, err := regexp.MatchString(r3, lines[5]); err != nil || !ok {
                        t.Errorf("%q didn't match %q", lines[5], r3)
                }
-               t.Logf(prof)
+               t.Log(prof)
        })
        t.Run("proto", func(t *testing.T) {
                // proto format