]> Cypherpunks.ru repositories - gostls13.git/commitdiff
internal/profile: actually return errors in postDecode
authorDaniel Martí <mvdan@mvdan.cc>
Fri, 7 Apr 2023 17:53:49 +0000 (18:53 +0100)
committerDaniel Martí <mvdan@mvdan.cc>
Thu, 26 Oct 2023 07:37:45 +0000 (07:37 +0000)
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 <drchase@google.com>
Reviewed-by: Michael Pratt <mpratt@google.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>

src/internal/profile/encode.go
src/runtime/pprof/proto_test.go

index 77d77f1dfb5c34f40e65bc999ca183a8c9d9dba8..72d6fe2fa743bd8651ae427cc09adb943f0bcefe 100644 (file)
@@ -207,6 +207,9 @@ var profileDecoder = []decoder{
 // suffix X) and populates the corresponding exported fields.
 // The unexported fields are cleared up to facilitate testing.
 func (p *Profile) postDecode() error {
+       if p.Empty() {
+               return nil
+       }
        var err error
 
        mappings := make(map[uint64]*Mapping)
@@ -291,7 +294,7 @@ func (p *Profile) postDecode() error {
        p.commentX = nil
        p.DefaultSampleType, err = getString(p.stringTable, &p.defaultSampleTypeX, err)
        p.stringTable = nil
-       return nil
+       return err
 }
 
 func (p *ValueType) decoder() []decoder {
index f788b167da3ea9b9588660d0a9167d73d6826935..e1a7f2306dbf4570a1dd159d9bdb6435a4874f84 100644 (file)
@@ -64,13 +64,9 @@ func TestConvertCPUProfileEmpty(t *testing.T) {
        }
 
        // Expected PeriodType and SampleType.
-       periodType := &profile.ValueType{Type: "cpu", Unit: "nanoseconds"}
-       sampleType := []*profile.ValueType{
-               {Type: "samples", Unit: "count"},
-               {Type: "cpu", Unit: "nanoseconds"},
-       }
+       sampleType := []*profile.ValueType{{}, {}}
 
-       checkProfile(t, p, 2000*1000, periodType, sampleType, nil, "")
+       checkProfile(t, p, 2000*1000, nil, sampleType, nil, "")
 }
 
 func f1() { f1() }