Skip to content

Commit 5fe2035

Browse files
committed
internal/profile: actually return errors in postDecode
As spotted by staticcheck, the body did keep track of errors by sharing a single err variable, but its last value was never used as the function simply finished by returning nil. To prevent postDecode from erroring on empty profiles, which breaks TestEmptyProfile, add a check at the top of the function. Update the runtime/pprof test accordingly, since the default units didn't make sense for an empty profile anyway. Change-Id: I188cd8337434adf9169651ab5c914731b8b20f39 Reviewed-on: https://go-review.googlesource.com/c/go/+/483137 Reviewed-by: David Chase <[email protected]> Reviewed-by: Michael Pratt <[email protected]> Reviewed-by: Cherry Mui <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 555af99 commit 5fe2035

File tree

2 files changed

+6
-7
lines changed

2 files changed

+6
-7
lines changed

src/internal/profile/encode.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,9 @@ var profileDecoder = []decoder{
207207
// suffix X) and populates the corresponding exported fields.
208208
// The unexported fields are cleared up to facilitate testing.
209209
func (p *Profile) postDecode() error {
210+
if p.Empty() {
211+
return nil
212+
}
210213
var err error
211214

212215
mappings := make(map[uint64]*Mapping)
@@ -291,7 +294,7 @@ func (p *Profile) postDecode() error {
291294
p.commentX = nil
292295
p.DefaultSampleType, err = getString(p.stringTable, &p.defaultSampleTypeX, err)
293296
p.stringTable = nil
294-
return nil
297+
return err
295298
}
296299

297300
func (p *ValueType) decoder() []decoder {

src/runtime/pprof/proto_test.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,9 @@ func TestConvertCPUProfileEmpty(t *testing.T) {
6464
}
6565

6666
// Expected PeriodType and SampleType.
67-
periodType := &profile.ValueType{Type: "cpu", Unit: "nanoseconds"}
68-
sampleType := []*profile.ValueType{
69-
{Type: "samples", Unit: "count"},
70-
{Type: "cpu", Unit: "nanoseconds"},
71-
}
67+
sampleType := []*profile.ValueType{{}, {}}
7268

73-
checkProfile(t, p, 2000*1000, periodType, sampleType, nil, "")
69+
checkProfile(t, p, 2000*1000, nil, sampleType, nil, "")
7470
}
7571

7672
func f1() { f1() }

0 commit comments

Comments
 (0)