Skip to content

Commit

Permalink
fix: Solve panic on an empty profile ingest (#793)
Browse files Browse the repository at this point in the history
  • Loading branch information
kavu committed Feb 3, 2022
1 parent 4978fbd commit 2d3a479
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
6 changes: 5 additions & 1 deletion pkg/convert/parser.go
Expand Up @@ -4,6 +4,7 @@ import (
"bufio"
"bytes"
"compress/gzip"
"fmt"
"io"
"strconv"

Expand Down Expand Up @@ -34,8 +35,9 @@ func ParsePprof(r io.Reader) (*tree.Profile, error) {
bufioReader := bufio.NewReader(r)
header, err := bufioReader.Peek(2)
if err != nil {
return nil, err
return nil, fmt.Errorf("unable to read profile file header: %w", err)
}

if header[0] == gzipMagicBytes[0] && header[1] == gzipMagicBytes[1] {
r, err = gzip.NewReader(bufioReader)
if err != nil {
Expand All @@ -49,10 +51,12 @@ func ParsePprof(r io.Reader) (*tree.Profile, error) {
if err != nil {
return nil, err
}

profile := &tree.Profile{}
if err := proto.Unmarshal(b, profile); err != nil {
return nil, err
}

return profile, nil
}

Expand Down
19 changes: 15 additions & 4 deletions pkg/server/ingest.go
Expand Up @@ -65,17 +65,28 @@ func (h ingestHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if err == nil {
var profile *tree.Profile
var prevProfile *tree.Profile

f, _, err := r.FormFile("profile")
if err == nil {
profile, err = convert.ParsePprof(f)
if err != nil {
WriteError(h.log, w, http.StatusInternalServerError, err, "error happened while getting profile file")
return
}

profile, err = convert.ParsePprof(f)
if err != nil {
WriteError(h.log, w, http.StatusInternalServerError, err, "error happened while parsing profile file")
return
}

f, _, err = r.FormFile("prev_profile")
if err == nil {
prevProfile, err = convert.ParsePprof(f)
if err != nil {
WriteError(h.log, w, http.StatusInternalServerError, err, "error happened while parsing prev_profile file")
return
}
}

// TODO: add error handling for all of these

for _, sampleTypeStr := range profile.SampleTypes() {
var t *tree.SampleTypeConfig
var ok bool
Expand Down

0 comments on commit 2d3a479

Please sign in to comment.