From eaf57e8f559aec9d73cb1d6d3ba203ffa184b1a3 Mon Sep 17 00:00:00 2001 From: Arthur Gautier Date: Wed, 3 Sep 2014 15:21:06 +0200 Subject: [PATCH] Fix SEGFAULT if dns resolv error Per registry.doRequest, res and client might be nil in case of error For example, dns resolution errors, /etc/docker/certs.d perms, failed loading of x509 cert ... This will make res.StatusCode and res.Body SEGFAULT. Signed-off-by: Arthur Gautier --- docs/session.go | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/docs/session.go b/docs/session.go index a8ade7053..c78e559b4 100644 --- a/docs/session.go +++ b/docs/session.go @@ -153,10 +153,11 @@ func (r *Session) GetRemoteImageJSON(imgID, registry string, token []string) ([] func (r *Session) GetRemoteImageLayer(imgID, registry string, token []string, imgSize int64) (io.ReadCloser, error) { var ( - retries = 5 - client *http.Client - res *http.Response - imageURL = fmt.Sprintf("%simages/%s/layer", registry, imgID) + retries = 5 + statusCode = 0 + client *http.Client + res *http.Response + imageURL = fmt.Sprintf("%simages/%s/layer", registry, imgID) ) req, err := r.reqFactory.NewRequest("GET", imageURL, nil) @@ -165,14 +166,19 @@ func (r *Session) GetRemoteImageLayer(imgID, registry string, token []string, im } setTokenAuth(req, token) for i := 1; i <= retries; i++ { + statusCode = 0 res, client, err = r.doRequest(req) if err != nil { - if res.Body != nil { - res.Body.Close() + log.Debugf("Error contacting registry: %s", err) + if res != nil { + if res.Body != nil { + res.Body.Close() + } + statusCode = res.StatusCode } if i == retries { return nil, fmt.Errorf("Server error: Status %d while fetching image layer (%s)", - res.StatusCode, imgID) + statusCode, imgID) } time.Sleep(time.Duration(i) * 5 * time.Second) continue