Browse code

*: fix response body leaks

Signed-off-by: Antonio Murdaca <runcom@redhat.com>

Antonio Murdaca authored on 2016/03/17 00:38:13
Showing 5 changed files
... ...
@@ -236,6 +236,7 @@ func (cli *DockerCli) CmdBuild(args ...string) error {
236 236
 	if err != nil {
237 237
 		return err
238 238
 	}
239
+	defer response.Body.Close()
239 240
 
240 241
 	err = jsonmessage.DisplayJSONMessagesStream(response.Body, buildBuff, cli.outFd, cli.isTerminalOut, nil)
241 242
 	if err != nil {
... ...
@@ -161,7 +161,6 @@ func (d *graphDriverProxy) Diff(id, parent string) (archive.Archive, error) {
161 161
 	}
162 162
 	body, err := d.client.Stream("GraphDriver.Diff", args)
163 163
 	if err != nil {
164
-		body.Close()
165 164
 		return nil, err
166 165
 	}
167 166
 	return archive.Archive(body), nil
... ...
@@ -174,9 +174,7 @@ func (l *splunkLogger) Log(msg *logger.Message) error {
174 174
 	if err != nil {
175 175
 		return err
176 176
 	}
177
-	if res.Body != nil {
178
-		defer res.Body.Close()
179
-	}
177
+	defer res.Body.Close()
180 178
 	if res.StatusCode != http.StatusOK {
181 179
 		var body []byte
182 180
 		body, err = ioutil.ReadAll(res.Body)
... ...
@@ -101,6 +101,7 @@ func (c *Client) SendFile(serviceMethod string, data io.Reader, ret interface{})
101 101
 	if err != nil {
102 102
 		return err
103 103
 	}
104
+	defer body.Close()
104 105
 	if err := json.NewDecoder(body).Decode(&ret); err != nil {
105 106
 		logrus.Errorf("%s: error reading plugin resp: %v", serviceMethod, err)
106 107
 		return err
... ...
@@ -284,6 +284,7 @@ func (r *Session) GetRemoteImageLayer(imgID, registry string, imgSize int64) (io
284 284
 	res, err = r.client.Do(req)
285 285
 	if err != nil {
286 286
 		logrus.Debugf("Error contacting registry %s: %v", registry, err)
287
+		// the only case err != nil && res != nil is https://golang.org/src/net/http/client.go#L515
287 288
 		if res != nil {
288 289
 			if res.Body != nil {
289 290
 				res.Body.Close()