]> Cypherpunks repositories - gostls13.git/commitdiff
std: pass bytes.Buffer and strings.Builder by pointer
authorAlan Donovan <adonovan@google.com>
Wed, 11 Dec 2024 20:42:06 +0000 (15:42 -0500)
committerGopher Robot <gobot@golang.org>
Mon, 19 May 2025 16:13:04 +0000 (09:13 -0700)
This CL fixes a number of (all true positive) findings of vet's
copylock analyzer patched to treat the Bu{ff,uild}er types
as non-copyable after first use.

This does require imposing an additional indirection
between noder.writer and Encoder since the field is
embedded by value but its constructor now returns a pointer.

Updates golang/go#25907
Updates golang/go#47276

Change-Id: I0b4d77ac12bcecadf06a91709e695365da10766c
Reviewed-on: https://go-review.googlesource.com/c/go/+/635339
Reviewed-by: Robert Findley <rfindley@google.com>
Commit-Queue: Alan Donovan <adonovan@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Alan Donovan <adonovan@google.com>

src/bytes/buffer_test.go
src/cmd/compile/internal/noder/linker.go
src/cmd/compile/internal/noder/writer.go
src/compress/flate/inflate_test.go
src/internal/pkgbits/encoder.go
src/runtime/pprof/pprof_test.go

index 97fca5a9d136f99517a7aca93511bfb1aa8ad685..b46ba1204eb8066f949ef0ddc2d7f0719e7ab8a2 100644 (file)
@@ -354,7 +354,7 @@ func TestWriteAppend(t *testing.T) {
                got.Write(b)
        }
        if !Equal(got.Bytes(), want) {
-               t.Fatalf("Bytes() = %q, want %q", got, want)
+               t.Fatalf("Bytes() = %q, want %q", &got, want)
        }
 
        // With a sufficiently sized buffer, there should be no allocations.
index 3e60e998501d4793cac9d53965e1f4426f1962a7..6ee29a205b833319475dde306b399f1d7555519d 100644 (file)
@@ -84,7 +84,7 @@ func (l *linker) relocIdx(pr *pkgReader, k pkgbits.SectionKind, idx index) index
                // if we do external relocations.
 
                w := l.pw.NewEncoderRaw(k)
-               l.relocCommon(pr, &w, k, idx)
+               l.relocCommon(pr, w, k, idx)
                newidx = w.Idx
        }
 
@@ -168,9 +168,9 @@ func (l *linker) relocObj(pr *pkgReader, idx index) index {
        assert(wname.Idx == w.Idx)
        assert(wdict.Idx == w.Idx)
 
-       l.relocCommon(pr, &w, pkgbits.SectionObj, idx)
-       l.relocCommon(pr, &wname, pkgbits.SectionName, idx)
-       l.relocCommon(pr, &wdict, pkgbits.SectionObjDict, idx)
+       l.relocCommon(pr, w, pkgbits.SectionObj, idx)
+       l.relocCommon(pr, wname, pkgbits.SectionName, idx)
+       l.relocCommon(pr, wdict, pkgbits.SectionObjDict, idx)
 
        // Generic types and functions won't have definitions, and imported
        // objects may not either.
@@ -181,15 +181,15 @@ func (l *linker) relocObj(pr *pkgReader, idx index) index {
                wext.Sync(pkgbits.SyncObject1)
                switch tag {
                case pkgbits.ObjFunc:
-                       l.relocFuncExt(&wext, obj)
+                       l.relocFuncExt(wext, obj)
                case pkgbits.ObjType:
-                       l.relocTypeExt(&wext, obj)
+                       l.relocTypeExt(wext, obj)
                case pkgbits.ObjVar:
-                       l.relocVarExt(&wext, obj)
+                       l.relocVarExt(wext, obj)
                }
                wext.Flush()
        } else {
-               l.relocCommon(pr, &wext, pkgbits.SectionObjExt, idx)
+               l.relocCommon(pr, wext, pkgbits.SectionObjExt, idx)
        }
 
        // Check if we need to export the inline bodies for functions and
index 60a13108bc0d94c4b7522327d57c94bb3518bb96..dd79c3ef4c87cbbcdf48e63c31e160003ebd2c2c 100644 (file)
@@ -174,7 +174,7 @@ func (pw *pkgWriter) typeOf(expr syntax.Expr) types2.Type {
 type writer struct {
        p *pkgWriter
 
-       pkgbits.Encoder
+       *pkgbits.Encoder
 
        // sig holds the signature for the current function body, if any.
        sig *types2.Signature
index 28a0122ac68cc34cb38b5e54f634921c1cb3b6f2..064c832d3df654b000dc54b5ab2096022cf54740 100644 (file)
@@ -35,7 +35,7 @@ func TestReset(t *testing.T) {
 
        for i, s := range ss {
                if s != inflated[i].String() {
-                       t.Errorf("inflated[%d]:\ngot  %q\nwant %q", i, inflated[i], s)
+                       t.Errorf("inflated[%d]:\ngot  %q\nwant %q", i, &inflated[i], s)
                }
        }
 }
@@ -92,7 +92,7 @@ func TestResetDict(t *testing.T) {
 
        for i, s := range ss {
                if s != inflated[i].String() {
-                       t.Errorf("inflated[%d]:\ngot  %q\nwant %q", i, inflated[i], s)
+                       t.Errorf("inflated[%d]:\ngot  %q\nwant %q", i, &inflated[i], s)
                }
        }
 }
index 1b384690978e50dc8b6f457978ff9caedad2c1bb..5c51642e3c4f4d8b797ac74e53188bf2adbde5c9 100644 (file)
@@ -121,7 +121,7 @@ func (pw *PkgEncoder) StringIdx(s string) RelIndex {
 // NewEncoder returns an Encoder for a new element within the given
 // section, and encodes the given SyncMarker as the start of the
 // element bitstream.
-func (pw *PkgEncoder) NewEncoder(k SectionKind, marker SyncMarker) Encoder {
+func (pw *PkgEncoder) NewEncoder(k SectionKind, marker SyncMarker) *Encoder {
        e := pw.NewEncoderRaw(k)
        e.Sync(marker)
        return e
@@ -131,11 +131,11 @@ func (pw *PkgEncoder) NewEncoder(k SectionKind, marker SyncMarker) Encoder {
 // section.
 //
 // Most callers should use NewEncoder instead.
-func (pw *PkgEncoder) NewEncoderRaw(k SectionKind) Encoder {
+func (pw *PkgEncoder) NewEncoderRaw(k SectionKind) *Encoder {
        idx := RelIndex(len(pw.elems[k]))
        pw.elems[k] = append(pw.elems[k], "") // placeholder
 
-       return Encoder{
+       return &Encoder{
                p:   pw,
                k:   k,
                Idx: idx,
index 6f9446a745afe1a1085652243870d4426bb20b43..f2ee39dd49696c5441a3578f58a6148902274773 100644 (file)
@@ -465,7 +465,7 @@ func testCPUProfile(t *testing.T, matches profileMatchFunc, f func(dur time.Dura
                f(duration)
                StopCPUProfile()
 
-               if p, ok := profileOk(t, matches, prof, duration); ok {
+               if p, ok := profileOk(t, matches, &prof, duration); ok {
                        return p
                }
 
@@ -515,7 +515,7 @@ func stackContains(spec string, count uintptr, stk []*profile.Location, labels m
 
 type sampleMatchFunc func(spec string, count uintptr, stk []*profile.Location, labels map[string][]string) bool
 
-func profileOk(t *testing.T, matches profileMatchFunc, prof bytes.Buffer, duration time.Duration) (_ *profile.Profile, ok bool) {
+func profileOk(t *testing.T, matches profileMatchFunc, prof *bytes.Buffer, duration time.Duration) (_ *profile.Profile, ok bool) {
        ok = true
 
        var samples uintptr