Sfoglia il codice sorgente

Explicitly return error for empty input files

Often profile handlers return no data, and pprof currently
returns "profile format unrecognized", which confuses users
and sends them on a wild goose chase.

Print a more explicit "empty input file" error message.
Raul Silvera 8 anni fa
parent
commit
02c8df458d
1 ha cambiato i file con 10 aggiunte e 4 eliminazioni
  1. 10
    4
      profile/profile.go

+ 10
- 4
profile/profile.go Vedi File

@@ -155,10 +155,12 @@ func ParseData(data []byte) (*Profile, error) {
155 155
 			return nil, fmt.Errorf("decompressing profile: %v", err)
156 156
 		}
157 157
 	}
158
-	if p, err = ParseUncompressed(data); err != nil {
159
-		if p, err = parseLegacy(data); err != nil {
160
-			return nil, fmt.Errorf("parsing profile: %v", err)
161
-		}
158
+	if p, err = ParseUncompressed(data); err != nil && err != errNoData {
159
+		p, err = parseLegacy(data)
160
+	}
161
+
162
+	if err != nil {
163
+		return nil, fmt.Errorf("parsing profile: %v", err)
162 164
 	}
163 165
 
164 166
 	if err := p.CheckValid(); err != nil {
@@ -169,6 +171,7 @@ func ParseData(data []byte) (*Profile, error) {
169 171
 
170 172
 var errUnrecognized = fmt.Errorf("unrecognized profile format")
171 173
 var errMalformed = fmt.Errorf("malformed profile format")
174
+var errNoData = fmt.Errorf("empty input file")
172 175
 
173 176
 func parseLegacy(data []byte) (*Profile, error) {
174 177
 	parsers := []func([]byte) (*Profile, error){
@@ -195,6 +198,9 @@ func parseLegacy(data []byte) (*Profile, error) {
195 198
 
196 199
 // ParseUncompressed parses an uncompressed protobuf into a profile.
197 200
 func ParseUncompressed(data []byte) (*Profile, error) {
201
+	if len(data) == 0 {
202
+		return nil, errNoData
203
+	}
198 204
 	p := &Profile{}
199 205
 	if err := unmarshal(data, p); err != nil {
200 206
 		return nil, err