Skip to content

Commit 52bd3b1

Browse files
thanmmdempsky
authored andcommitted
[release-branch.go1.20] internal/coverage/decodemeta: fix coding error in func literal handling
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 <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Cherry Mui <[email protected]> (cherry picked from commit 620399e) Reviewed-on: https://go-review.googlesource.com/c/go/+/463418 Reviewed-by: Than McIntosh <[email protected]> Run-TryBot: Matthew Dempsky <[email protected]> Auto-Submit: Matthew Dempsky <[email protected]>
1 parent be7e4fe commit 52bd3b1

File tree

3 files changed

+57
-3
lines changed

3 files changed

+57
-3
lines changed

src/cmd/covdata/dump.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ func (d *dstate) VisitFunc(pkgIdx uint32, fnIdx uint32, fd *coverage.FuncDesc) {
288288
}
289289
fmt.Printf("\nFunc: %s\n", fd.Funcname)
290290
fmt.Printf("Srcfile: %s\n", fd.Srcfile)
291+
fmt.Printf("Literal: %v\n", fd.Lit)
291292
}
292293
for i := 0; i < len(fd.Units); i++ {
293294
u := fd.Units[i]

src/internal/coverage/decodemeta/decode.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,6 @@ func (d *CoverageMetaDataDecoder) ReadFunc(fidx uint32, f *coverage.FuncDesc) er
123123
})
124124
}
125125
lit := d.r.ReadULEB128()
126-
if lit != 0 {
127-
f.Lit = true
128-
}
126+
f.Lit = lit != 0
129127
return nil
130128
}

src/internal/coverage/test/roundtrip_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,3 +274,58 @@ func TestMetaDataWriterReader(t *testing.T) {
274274
inf.Close()
275275
}
276276
}
277+
278+
func TestMetaDataDecodeLitFlagIssue57942(t *testing.T) {
279+
280+
// Encode a package with a few functions. The funcs alternate
281+
// between regular functions and function literals.
282+
pp := "foo/bar/pkg"
283+
pn := "pkg"
284+
mp := "barmod"
285+
b, err := encodemeta.NewCoverageMetaDataBuilder(pp, pn, mp)
286+
if err != nil {
287+
t.Fatalf("making builder: %v", err)
288+
}
289+
const NF = 6
290+
const NCU = 1
291+
ln := uint32(10)
292+
wantfds := []coverage.FuncDesc{}
293+
for fi := uint32(0); fi < NF; fi++ {
294+
fis := fmt.Sprintf("%d", fi)
295+
fd := coverage.FuncDesc{
296+
Funcname: "func" + fis,
297+
Srcfile: "foo" + fis + ".go",
298+
Units: []coverage.CoverableUnit{
299+
coverage.CoverableUnit{StLine: ln + 1, StCol: 2, EnLine: ln + 3, EnCol: 4, NxStmts: fi + 2},
300+
},
301+
Lit: (fi % 2) == 0,
302+
}
303+
wantfds = append(wantfds, fd)
304+
b.AddFunc(fd)
305+
}
306+
307+
// Emit into a writer.
308+
drws := &slicewriter.WriteSeeker{}
309+
b.Emit(drws)
310+
311+
// Decode the result.
312+
drws.Seek(0, io.SeekStart)
313+
dec, err := decodemeta.NewCoverageMetaDataDecoder(drws.BytesWritten(), false)
314+
if err != nil {
315+
t.Fatalf("making decoder: %v", err)
316+
}
317+
nf := dec.NumFuncs()
318+
if nf != NF {
319+
t.Fatalf("decoder number of functions: got %d want %d", nf, NF)
320+
}
321+
var fn coverage.FuncDesc
322+
for i := uint32(0); i < uint32(NF); i++ {
323+
if err := dec.ReadFunc(i, &fn); err != nil {
324+
t.Fatalf("err reading function %d: %v", i, err)
325+
}
326+
res := cmpFuncDesc(wantfds[i], fn)
327+
if res != "" {
328+
t.Errorf("ReadFunc(%d): %s", i, res)
329+
}
330+
}
331+
}

0 commit comments

Comments
 (0)