]> Cypherpunks repositories - gostls13.git/commitdiff
internal/coverage/decodemeta: fix coding error in func literal handling
authorThan McIntosh <thanm@google.com>
Fri, 20 Jan 2023 19:03:43 +0000 (14:03 -0500)
committerThan McIntosh <thanm@google.com>
Fri, 20 Jan 2023 21:42:05 +0000 (21:42 +0000)
Fix a coding error in coverage meta-data decoding in the method
decodemeta.CoverageMetaDataDecoder.ReadFunc. The code was not
unconditionally assigning the "function literal" field of the
coverage.FuncDesc object passed in, resulting in bad values depending
on what the state of the field happened to be in the object.

Fixes #57942.

Change-Id: I6dfd7d7f7af6004f05c622f9a7116e9f6018cf4f
Reviewed-on: https://go-review.googlesource.com/c/go/+/462955
Run-TryBot: Than McIntosh <thanm@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Cherry Mui <cherryyz@google.com>
src/cmd/covdata/dump.go
src/internal/coverage/decodemeta/decode.go
src/internal/coverage/test/roundtrip_test.go

index 59fdc80d03d6df92e84847bfd4436c2566ac7ab6..62267170ce1d1295cd1ed46493c1faea603ae702 100644 (file)
@@ -288,6 +288,7 @@ func (d *dstate) VisitFunc(pkgIdx uint32, fnIdx uint32, fd *coverage.FuncDesc) {
                }
                fmt.Printf("\nFunc: %s\n", fd.Funcname)
                fmt.Printf("Srcfile: %s\n", fd.Srcfile)
+               fmt.Printf("Literal: %v\n", fd.Lit)
        }
        for i := 0; i < len(fd.Units); i++ {
                u := fd.Units[i]
index 4e80c07f0c56847e99c48fcc0597e3b955016540..71f1c567ab0d0929d03409b20eeb998fb9b12f2d 100644 (file)
@@ -123,8 +123,6 @@ func (d *CoverageMetaDataDecoder) ReadFunc(fidx uint32, f *coverage.FuncDesc) er
                        })
        }
        lit := d.r.ReadULEB128()
-       if lit != 0 {
-               f.Lit = true
-       }
+       f.Lit = lit != 0
        return nil
 }
index b26993ffd5825d8cf1a4bfe43c55a7e37e74da82..614f56e6329a35b58a860eb74928402d497c5d7c 100644 (file)
@@ -274,3 +274,58 @@ func TestMetaDataWriterReader(t *testing.T) {
                inf.Close()
        }
 }
+
+func TestMetaDataDecodeLitFlagIssue57942(t *testing.T) {
+
+       // Encode a package with a few functions. The funcs alternate
+       // between regular functions and function literals.
+       pp := "foo/bar/pkg"
+       pn := "pkg"
+       mp := "barmod"
+       b, err := encodemeta.NewCoverageMetaDataBuilder(pp, pn, mp)
+       if err != nil {
+               t.Fatalf("making builder: %v", err)
+       }
+       const NF = 6
+       const NCU = 1
+       ln := uint32(10)
+       wantfds := []coverage.FuncDesc{}
+       for fi := uint32(0); fi < NF; fi++ {
+               fis := fmt.Sprintf("%d", fi)
+               fd := coverage.FuncDesc{
+                       Funcname: "func" + fis,
+                       Srcfile:  "foo" + fis + ".go",
+                       Units: []coverage.CoverableUnit{
+                               coverage.CoverableUnit{StLine: ln + 1, StCol: 2, EnLine: ln + 3, EnCol: 4, NxStmts: fi + 2},
+                       },
+                       Lit: (fi % 2) == 0,
+               }
+               wantfds = append(wantfds, fd)
+               b.AddFunc(fd)
+       }
+
+       // Emit into a writer.
+       drws := &slicewriter.WriteSeeker{}
+       b.Emit(drws)
+
+       // Decode the result.
+       drws.Seek(0, io.SeekStart)
+       dec, err := decodemeta.NewCoverageMetaDataDecoder(drws.BytesWritten(), false)
+       if err != nil {
+               t.Fatalf("making decoder: %v", err)
+       }
+       nf := dec.NumFuncs()
+       if nf != NF {
+               t.Fatalf("decoder number of functions: got %d want %d", nf, NF)
+       }
+       var fn coverage.FuncDesc
+       for i := uint32(0); i < uint32(NF); i++ {
+               if err := dec.ReadFunc(i, &fn); err != nil {
+                       t.Fatalf("err reading function %d: %v", i, err)
+               }
+               res := cmpFuncDesc(wantfds[i], fn)
+               if res != "" {
+                       t.Errorf("ReadFunc(%d): %s", i, res)
+               }
+       }
+}